我有一个 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];
}];
}
最佳答案
如果我理解正确的话,您正在使用 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];
}];
}
关于ios - 当通过 UIRefreshControl 调用时,UITableView 重新加载会重复 NSMutableArray 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517374/