iphone - 从频繁变化的 NSArray 中刷新 UItableView 数据

标签 iphone objective-c ios cocoa-touch ios5

我有一个 UITableView 并且我正在从 Web 服务读取数据。 来自网络服务的数据可能随时发生变化,所以我必须定期刷新我的数据。

我已成功刷新数据并将其存储在 NSArray 中,但 UITableView 不会显示这些数据。

我试过了

[myTableView reloadData];

但没有效果。

编辑:

我已经实现了将数据从 NSArray 加载到 UiTableView 的所有方法。 当 NSArray 在 ViewDidLoad 中初始化时,此方法有效。

但是如果NSArray在应用程序运行时发生更改,UITableView将不会显示这些更改。

最佳答案

您需要实现UITableViewDataSource委托(delegate)协议(protocol),特别是- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

使用此方法来设置单元格。您可以使用 indexPathrow 属性来确定要设置的单元格并向其提供数组中的数据。

例如

- (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/

相关文章:

iphone - 无法删除开发者证书

ios - deleteObject 在 Realm 中不起作用

ios - 在 ios 中隐藏状态栏

ios - UICollectionView 中重复使用的单元格显示多个 UIImageView,而它们应该只显示一个

ios - 使用非默认配置时的 Realm 迁移

iphone - 启用 ARC lite 的应用程序仅在发布配置中在第二代 iPod 上崩溃

objective-c - 如何在 iOS 上删除 UIImageView 图像的某些部分?

iphone - UIView 子类不会自动调整大小

iphone - 从 NSDictionary 数组中获取值的数组

iOS:如何在tableview中响应显示参数?