我有一个 UITableView
并且我正在从 Web 服务读取数据。
来自网络服务的数据可能随时发生变化,所以我必须定期刷新我的数据。
我已成功刷新数据并将其存储在 NSArray
中,但 UITableView
不会显示这些数据。
我试过了
[myTableView reloadData];
但没有效果。
编辑:
我已经实现了将数据从 NSArray
加载到 UiTableView
的所有方法。
当 NSArray 在 ViewDidLoad 中初始化时,此方法有效。
但是如果NSArray
在应用程序运行时发生更改,UITableView
将不会显示这些更改。
最佳答案
您需要实现UITableViewDataSource
委托(delegate)协议(protocol),特别是- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用此方法来设置单元格。您可以使用 indexPath
的 row
属性来确定要设置的单元格并向其提供数组中的数据。
例如
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
id item = [dataArray objectAtIndex:indexPath.row]; // Whatever data you're storing in your array
cell.textLabel.text = [item description]; // Substitute this for whatever you want to do with your cell.
}
编辑:
reloadData
应该调用
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
和
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
查看是否有任何单元格要绘制。确保您也实现这些方法并返回非零值,否则您的表格 View 不会尝试绘制任何单元格和 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
不会被调用。
解析
您可能还对this library感兴趣它声称使远程数据驱动表变得更加简单。
关于iphone - 从频繁变化的 NSArray 中刷新 UItableView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8692983/