baste
baste

Reputation: 827

UITableView reload duplicates the value of NSMutableArray when called via UIRefreshControl

I have an NSMutableArrays that keeps the JSON info I requested from the server. These are i use inside of my UITableViews (i have two for now). Whenever I called the UIRefreshControl it seems that my NSMutableArray just add up to the previous total of item inside. If the previous array count is 20 the next call to UIRefreshControl wil make it 40 to 80. This caused the UITableView to display duplicated values. I tried making my NSMutableArray to nil in some of the function (like before the refresh code) but no luck. Any ideas please help.

Here is part of my code:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    if (tableView==tblMessaging) {
        //messageArrays is my NSMutableArray for this UITableView
        messageDict =[messageArrays objectAtIndex:indexPath.row];
        int statusMsg=[[messageDict objectForKey:@"Status"]intValue];
        PimmMessage *message=[messageDict objectForKey:@"Message"];

        lblMessageBody=(UILabel *)[cell viewWithTag:202];
        [lblMessageBody setText:[TMSGlobalFunctions decodeBase64String:message.body]];

        lblMessageTime=(UILabel *)[cell viewWithTag:201];

        [lblMessageTime setText:[TMSGlobalFunctions strDate:message.sentUTC]];

    }

    UIView *bgcolor=[[UIView alloc]init];
    [bgcolor setBackgroundColor:[UIColor colorWithRed:255/255.0f green:159.0/255.0f blue:0.0/255.0f alpha:.7]];
    bgcolor.layer.cornerRadius=1;
    [cell setSelectedBackgroundView:bgcolor];

  return cell;    
}

    -(void)checkMessage
{
 __block NSArray *nsArrSortedMessages = [[NSArray alloc] init];

    [ipimm getMessageListForRecipientUser:globUserID SenderUser:nil Status:-1 Priority:-1 StartTime:nil EndTime:nil Callback:^(NSArray *pimmMessageList, NSError *err, int requestIdentifier) {

            for(PimmMessage *message in pimmMessageList) {
                 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

                NSLog(@"message contnent %@", message.body);
            [dictionary setObject:[NSString stringWithFormat:@"%d",message.status] forKey:@"Status"];
            [dictionary setObject:message.sentUTC forKey:@"SentUTC"];
            [dictionary setObject:message forKey:@"Message"];
            [messageArrays addObject:dictionary];
        }

        NSLog(@"messageArray %d", [messageArrays count]);
        NSSortDescriptor *sortMessage = [[NSSortDescriptor alloc] initWithKey:@"SentUTC" ascending:NO];
        nsArrSortedMessages = [messageArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortMessage]];
        [tblMessaging reloadData];


    }];


}

Upvotes: 0

Views: 337

Answers (2)

Matt
Matt

Reputation: 2411

You should also check out the Sensible TableView framework, which will automatically fetch your JSON data, and will handle displaying in table view, refresh, etc. Saves me tons of time.

Upvotes: 0

Yan
Yan

Reputation: 3616

If i understand correctly you are using messageArrays as your datasource for your tableView. Every time you call -(void)checkMessage you are adding object to the messageArrays. Is that right or you really want to use nsArrSortedMessages as your data source for the table.

  -(void)checkMessage
    {


        [ipimm getMessageListForRecipientUser:globUserID SenderUser:nil Status:-1 Priority:-1 StartTime:nil EndTime:nil Callback:^(NSArray *pimmMessageList, NSError *err, int requestIdentifier) {
NSMutableArray *tempMessages = [[NSArray alloc] init];

                for(PimmMessage *message in pimmMessageList) {
                     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

                    NSLog(@"message contnent %@", message.body);
                [dictionary setObject:[NSString stringWithFormat:@"%d",message.status] forKey:@"Status"];
                [dictionary setObject:message.sentUTC forKey:@"SentUTC"];
                [dictionary setObject:message forKey:@"Message"];
                [tempMessages addObject:dictionary];
            }

            NSLog(@"messageArray %d", [messageArrays count]);
            NSSortDescriptor *sortMessage = [[NSSortDescriptor alloc] initWithKey:@"SentUTC" ascending:NO];
            messageArrays = [tempMessages sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortMessage]];
            [tblMessaging reloadData];


        }];
    }

Upvotes: 1

Related Questions