2013-07-07 1 views
0

서버에서 요청한 JSON 정보를 유지하는 NSMutableArrays가 있습니다. 이들은 내 UITableViews 내부에 사용하고 있습니다 (나는 현재 2 개가 있습니다). UIRefreshControl을 호출 할 때마다 내 NSMutableArray가 내부의 이전 합계에 더할 것으로 보인다. 이전 배열 수가 20이면 UIRefreshControl에 대한 다음 호출은 40에서 80이됩니다. 이로 인해 UITableView는 중복 된 값을 표시합니다. NSMutableArray를 새로 고침 코드와 같은 일부 함수에서 무효로 만들려고했지만 행운이 없었습니다. 아이디어가 도움이 되십시오. 여기UITableView reload는 UIRefreshControl을 통해 호출 될 때 NSMutableArray의 값을 복제합니다.

내 코드의 일부입니다 : 내가 정확하게 당신이 당신의있는 tableView에 대한 데이터 소스로 messageArrays를 사용하고 이해한다면

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


    }]; 


} 

답변

1

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


     }]; 
    } 
+0

예 선생님, messageArrays를 데이터 소스로 사용하여 nsArrSortedMessages에 주석을 달았지만 여전히 같은 결과가 나타납니다. 감사. 어떻게 messageArrays 내부의 오래된 객체를 제거 할 수 있습니까? – baste

+0

checkMessage를 호출 할 때마다 데이터를 다시로드합니까? 그렇다면 당신은 마지막에 messageArray를 동일하게 설정해야합니다. nsArrSortedMessages – Yan

+0

내 대답의 업데이트를 확인하십시오. checkMessage 함수에 대한 코드를 업데이트했습니다. – Yan

0

또한 자동으로 JSON 데이터를 가져옵니다, 등 테이블보기, 새로 고침에 표시하는 것은 나에게 시간의 톤을 저장 처리하는 분별있는 TableView 프레임 워크를 체크 아웃해야합니다.

관련 문제