2013-08-29 35 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 ] ..设置好所有单元格内容后,重新载入数据,它将刷新tableview。

+0

我只是添加了方法[self.table relaodData],但它仍然崩溃。 – Tek

+0

您正在使用cellforrowatindexpath设置特定行的数据?您有多少行以及您设置数据的行数?如果您假设有10行,并且您将数据设置为1行,则会崩溃。尝试为所有行设置数据,然后尝试在cellforrowatindexpath方法中执行json数据存储和数据设置。在那里你不能错过任何细胞。 – NHS

+0

我得到了应用程序因您的评论而崩溃的原因。但我不知道为所有行设置数据。在'-(UITableViewCell *)tableView:'方法中,它将帮助我将所有数据设置到每个单元格。我应该在按钮动作中使用for循环来实现效果吗? 我是初学者,希望你不要介意这些微不足道的问题。 – Tek

0

我不知道如何从[self sendTOcompareByJSON:jsonData];获取数据。如果它是一个同步调用,到web服务器,那么你可以在这之后更新你的数据源(在你使用listData的情况下你正在填充tableview)。所以一旦listData更新了新的内容,那么你应该像这样重新加载你的tableview [self.table reloadData]

如果对web服务器进行异步调用,则更新数据源并在回调中重新加载表。

希望这会有所帮助。