【问题标题】:UITableView reload duplicates the value of NSMutableArray when called via UIRefreshControl当通过 UIRefreshControl 调用时,UITableView 重新加载会重复 NSMutableArray 的值
【发布时间】:2013-07-05 05:43:35
【问题描述】:

我有一个 NSMutableArrays 来保存我从服务器请求的 JSON 信息。 这些是我在我的 UITableViews 中使用的(我现在有两个)。 每当我调用 UIRefreshControl 时,似乎我的 NSMutableArray 只是加起来就是里面的前一个项目总数。如果先前的数组计数为 20,则对 UIRefreshControl 的下一次调用将使其变为 40 到 80。 这导致 UITableView 显示重复值。 我尝试在某些函数中使我的 NSMutableArray 为零(例如在刷新代码之前),但没有运气。 有什么想法请帮忙。

这是我的代码的一部分:

    - (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];


    }];


}

【问题讨论】:

    标签: ios uitableview duplicates reload


    【解决方案1】:

    您还应该查看 Sensible TableView 框架,该框架将自动获取您的 JSON 数据,并处理在表格视图中显示、刷新等。为我节省了大量时间。

    【讨论】:

      【解决方案2】:

      如果我理解正确,您正在使用 messageArrays 作为 tableView 的数据源。每次调用 -(void)checkMessage 时,您都在向 messageArrays 添加对象。是这样吗?还是您真的想使用 nsArrSortedMessages 作为表的数据源。

        -(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];
      
      
              }];
          }
      

      【讨论】:

      • 是的,先生,我使用 messageArrays 作为我的数据源,我尝试评论 nsArrSortedMessages 但仍然是相同的结果。谢谢。如何删除 messageArrays 中的旧对象?
      • 每次调用 checkMessage 时都在重新加载数据吗?如果是,那么您最后必须将 messageArray 设置为等于 nsArrSortedMessages
      • 查看我的答案的更新。我更新了 checkMessage 函数的代码
      • 非常感谢它有效!我只需要将其 NSmutableArray 设为 tempMessages。
      • 太棒了。我更新了答案。我也不确定,但看看你的块是否在主线程上运行,如果你需要做 dispatch_get_main_queue() 来做 [tblMessaging reloadData];所有与 ui 相关的调用都必须在主线程上完成。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多