2013-08-29 2 views
0

내 앱에는 두 가지 구성 요소 인 UItableView와 UIButton이 있습니다.UIButton을 사용하여 UItableViewCell 내용 새로 고침

UItableViewcell은 JSON이 구현 한 원격 데이터베이스에서 데이터를로드합니다.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *TableIdentifier = @"tableidentifier" 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableIdentifier] autorelease]; 
} 
NSDictionary *voc_list=[listData objectAtIndex:indexPath.row]; 
NSLog(@"%@",voc_list); 
cell.textLabel.text = [[(NSDictionary*)voc_list objectForKey:@"vocabulary_list"]objectForKey:@"Vocabulary"]; 
cell.detailTextLabel.text=[[(NSDictionary*)voc_list objectForKey:@"vocabulary_list"]objectForKey:@"Translation"]; 

cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 


return cell; } 

그러나, 나는이 때 사용자 버튼을 눌러 모든 테이블 내용을 새로 고칠, 나는 다음과 같은 코드를 실행하려고 : 방금 전화하려고

-(IBAction)historyPressed:(id)sender{ 
    isToogle = !isToogle; 
    if(isToogle){ 
      // Back to original table content 

    }else{ 

     // Following codes will communicate with remote server and filter data to the app. 
     // The app go smooth here.  

     NSError *error = NULL; 
     NSDictionary *getStuID=[NSDictionary dictionaryWithObjectsAndKeys:student_id,@"Stu_ID", nil]; 
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:getStuID options:NSJSONWritingPrettyPrinted error:&error]; 
     [self sendTOcompareByJSON:jsonData]; 



     //Following codes are trying to show/refresh the data on tableview, but the app will go crash. 
     CGPoint location   = [sender locationInView:self.table]; 
     NSIndexPath *indexPath  = [self.table indexPathForRowAtPoint:location]; 
     UITableViewCell *new_cell=[self.table cellForRowAtIndexPath:indexPath]; 



     historyList_= [NSArray arrayWithArray:personalized_history]; 
     NSDictionary *dic = [historyList_ objectAtIndex:indexPath.row]; 
     new_cell.textLabel.text=[[(NSDictionary*)dic objectForKey:@"history_list"]objectForKey:@"Vocabulary"]; 
     new_cell.detailTextLabel.text=[[(NSDictionary*)dic objectForKey:@"history_lsit"]objectForKey:@"Score"]; 


    } 


    } 
+0

테이블의 데이터 소스에 사용 된 데이터를 업데이트하고 전체 테이블을 다시로드하는 간단한 옵션이 있습니다. – ratul

답변

0

historypressed 방법에 있어서는를 [yourtableview reloaddata ] .. 모든 셀 내용을 설정 한 후 다시로드하면 테이블 뷰가 새로 고쳐집니다.

+0

방금 ​​[self.table relaodData] 메소드를 추가했지만 여전히 충돌이 발생합니다. – Tek

+0

cellforrowatindexpath를 사용하여 특정 행에 대한 데이터를 설정하고 있습니까? 보유하고있는 행의 수와 데이터를 설정하는 행의 수는 얼마나됩니까? 10 행을 가정하고 1 행의 데이터를 설정하면 충돌이 발생합니다. 모든 행에 대한 데이터를 설정하려고 시도하십시오. cellforrowatindexpath 메소드에서 json 데이터 저장 및 설정을 시도하십시오. 거기에서 어떤 세포도 놓칠 수 없습니다. – NHS

+0

귀하의 의견에 따라 앱이 충돌하는 이유가 있습니다. 그러나 모든 행에 대해 데이터를 설정하지는 않습니다. '- (UITableViewCell *) tableView :'메서드에서는 각 셀에 모든 데이터를 설정하는 데 도움이됩니다. 효과를 얻기 위해 버튼 액션에서 for 루프를 사용해야합니까? 저는 초보자입니다.이 사소한 문제에 신경 쓰지 않기를 바랍니다. – Tek

0

[self sendTOcompareByJSON:jsonData];에서 데이터를 가져 오는 방법을 알지 못합니다. 그것의 동기화 호출, 웹 서버에 다음 그냥 (이 경우 listData을 사용하여 테이블 뷰를 작성하는 경우 귀하의 데이터 원본을 업데이트 할 수 있습니다.) 한 번 listData 새 내용으로 업데이트 한 다음 다음과 같이 테이블 뷰를 다시로드해야합니다. [self.table reloadData]

웹 서버에 대한 비동기 호출 인 경우 데이터 소스를 업데이트하고 콜백시 테이블을 다시로드하십시오.

희망이 도움이됩니다.