您的位置:首页 > 产品设计 > UI/UE

UITableView刷新某一行,改变数据源的问题

2015-12-21 16:23 302 查看
对于从字典中取值给cell赋值的,可以创建一个可变字典,每次从这个可变字典中取值赋值给cell,需要改变时,将这个可变字典中的值改变,然后再刷新即可。例如:

[WebConnect webPOSTConnectWithWithStrUrl:E_BindingUrl pramaters:nil success:^(id responseObject) {

NSLog(@"=====+++++++%@",responseObject);
NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"success"]];

if ([str isEqualToString:@"1"] ) {

NSString *bingState = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"bindState"]];

if ([bingState isEqualToString:@"0"]) {//未绑定状态

[_typeDic setObject:@"绑定" forKey:@"type"];
[_typeDic setObject:@"微信昵称" forKey:@"name"];

}else{//绑定状态

[_typeDic setObject:@"解除" forKey:@"type"];

NSString *name = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"nickName"]];
name = [name stringByRemovingPercentEncoding];

[_typeDic setObject:name forKey:@"name"];

}

}

dispatch_async(dispatch_get_main_queue(), ^{

NSIndexPath *te=[NSIndexPath indexPathForRow:0 inSection:0];//刷新第0行
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te,nil] withRowAnimation:UITableViewRowAnimationMiddle];
});

[MBProgressHUD hideHUDForView:self.view animated:YES];

} failure:^(id failure) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[MBProgressHUD showError:@"网络请求失败" toView:self.view];
}];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellId = @"cell";//唯一标示

_cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (_cell == 0) {
_cell = [[BindingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

_cell.delegate = self;

}

[_cell setCellAndRow:indexPath.row andDic:_typeDic];//每次都将这个字典赋值给cell

return _cell;

}


如果每次是数组赋值给cell,可以定义一个可变数组,每次刷新前给这个可变数组的数据源进行改变,如代码:
dispatch_async(dispatch_get_main_queue(), ^{

[self.tableView beginUpdates];

[self.dataSource replaceObjectAtIndex:i withObject:cellModel];

[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

[self.tableView endUpdates];

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: