详解iOS开发中UItableview控件的数据刷新功能的实现

2020-01-14 17:46:37王振洲

    cell.detailTextLabel.text=hero.intro;
    //4.返回cell
    return cell;
}

#pragma mark-数据刷新
//1.弹出窗口,拿到数据
//当某一行被选中的时候调用该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //拿到改行的数据模型
    YYheros *hero=self.heros[indexPath.row];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改数据" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    //密码框形式的
    //alert.alertViewStyle=UIAlertViewStyleSecureTextInput;
    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
    UITextField *text=[alert textFieldAtIndex:0];
    //把当前行的英雄数据显示到文本框中
    text.text=hero.name;
    alert.tag=indexPath.row;
    [alert show];
}
//2.修改数据,完成刷新操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //1.修改模型
    //如果选中的是取消,那么就返回,不做任何操作
    if (0==buttonIndex) return;
    //否则就修改模型,刷新数据
    YYheros *hero=self.heros[alertView.tag];
    
    //拿到当前弹窗中的文本数据(已经修改后的数据)
    UITextField *text=[alertView textFieldAtIndex:0];
    //用修改后的数据去修改模型
    hero.name=text.text;
   
    //2.刷新数据
    // 只要调用tableview的该方法就会自动重新调用数据源的所有方法
    // 会自动调用numberOfSectionsInTableView
    // 会自动调用numberOfRowsInSection
    // 会自动调用cellForRowAtIndexPath
    //    [self.tableview reloadData];
    
    // 刷新指定行
       NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
        [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
    //如果不进行刷新会怎么样?(修改之后不会即时刷新,要等到重新对cell进行数据填充的时候才会刷新)