我有一个 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];
}];
}