我有一個NSMutableArrays,它保持我從服務器請求的JSON信息。 這些是我在我的UITableViews內使用(我現在有兩個)。 每當我調用UIRefreshControl,看起來我的NSMutableArray只是加起來就是前面的項目總數。如果先前的數組數爲20,則下一次調用UIRefreshControl將使其成爲40到80. 這導致UITableView顯示重複的值。 我試着讓我的NSMutableArray在某些函數中(如刷新代碼之前)無效,但沒有運氣。 任何想法請幫助。UITableView重載通過UIRefreshControl調用時重複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作爲我的數據源,我試着評論nsArrSortedMessages但仍然是相同的結果。謝謝。 如何刪除messageArrays中的舊對象? – baste
您是否每次調用checkMessage時都重新加載數據?如果是,那麼你必須在最後設置messageArray等於nsArrSortedMessages – Yan
檢查我的答案的更新。我更新了你的checkMessage函數的代碼 – Yan