更新数据源并在 UITableView 上重新加载数据不反映更新的数据源
Posted
技术标签:
【中文标题】更新数据源并在 UITableView 上重新加载数据不反映更新的数据源【英文标题】:Update datasource and reload data on UITableView not reflecting updated datasource 【发布时间】:2016-01-12 14:12:19 【问题描述】:这是我的一段代码:
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary oldDict = (NSDictionary )[dataArray objectAtIndex:0];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:@"Don" forKey:@"Name"];
[dataArray replaceObjectAtIndex:0 withObject:newDict];
[self.tblview reloadData];
我必须重新加载我的tableview。但是tableview没有重新加载。如何纠正它。
【问题讨论】:
显示你的cellForRow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath if(tableView == self.tblviewProject) self.tblviewProject.hidden = YES; NSString *pname = [[self.projectArray objectAtIndex:indexPath.row] objectForKey:@"name"]; NSMutableDictionary *newDic = [[NSMutableDictionary alloc] init]; NSDictionary *rowchange = (NSDictionary *) [self.dataArray objectAtIndex:[self.projIndexPath intValue]]; [newDic addEntriesFromDictionary:rowchange]; [newDic setObject:pname forKey:project]; [self.tblview beginUpdates];
[self.dataArray replaceObjectAtIndex:[self.projIndexPath intValue] withObject:newDic]; [self.tblview reloadData]; [self.tblview endUpdates];
这是我的行单元格...现在我添加了 begin 和 endupdates 但它不起作用。
@RBH 不要将代码放入 cmets。将您的代码放入您的问题中。
【参考方案1】:
尝试开始和结束更新,因为您实际上并没有更改源数组。
而不是[self.tblview reloadData]
调用:
[self.tblview beginUpdates];
[self.tblview endUpdates];
或者,重新加载部分也应该在这种情况下工作。
【讨论】:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath if(tableView == self.tblviewProject) self.tblviewProject.hidden = YES; NSString *pname = [[self.projectArray objectAtIndex:indexPath.row] objectForKey:@"name"]; NSMutableDictionary *newDic = [[NSMutableDictionary alloc] init]; NSDictionary *rowchange = (NSDictionary *) [self.dataArray objectAtIndex:[self.projIndexPath intValue]]; [newDic addEntriesFromDictionary:rowchange]; [newDic setObject:pname forKey:project]; [self.tblview beginUpdates]; [self.dataArray replaceObjectAtIndex:[self.projIndexPath intValue] withObject:newDic]; [self.tblview reloadData]; [self.tblview endUpdates]; 但现在它对我不起作用...以上是关于更新数据源并在 UITableView 上重新加载数据不反映更新的数据源的主要内容,如果未能解决你的问题,请参考以下文章
UITableviewCell 在重新加载时显示旧数据和新数据
通知用户通过 UI xamarin iOS 在屏幕上接收来自服务器的更新时重新加载 uitableview 的最佳选择是啥?