使用UIButton刷新UItableViewCell内容

时间:2013-08-29 06:34:37

标签: objective-c uitableview uibutton

我的应用程序中有两个组件,UItableView和UIButton。

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


    }


    }

2 个答案:

答案 0 :(得分:0)

在historypressed方法中,只需尝试调用[yourtableview reloaddata] ..设置完所有单元格内容后重新加载数据,它将刷新tableview。

答案 1 :(得分:0)

我不知道你是如何从[self sendTOcompareByJSON:jsonData];获取数据的。如果是对网络服务器进行同步调用,那么您可以在此之后更新您的数据源(在您使用listData填充tableview的情况下)。一旦使用新内容更新listData,然后你应该重新加载你的tableview,如[self.table reloadData]

如果是对Web服务器的异步调用,那么请更新数据源并在回调时重新加载表。

希望这有帮助。