【发布时间】:2014-10-03 10:30:38
【问题描述】:
首先我使用 initWithCoder 初始化表格,然后将数据加载到单元格中。当数据源更改(这是 Web 服务)时,我希望重新加载表。只是为了测试我连接了按钮操作并添加了[self.tableView reloadData]
但是表没有重新加载,但数据源已更改。如果我转到不同的视图并返回表视图,则会显示新数据。有什么建议吗?
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.titleList = [[SearchModel alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self.titleList load: ^(id json) {
[self.tableView reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.tableView.delegate = self;
self.tableView.dataSource = self;
static NSString *CellIdentifier = @"title";
TitleDetailCell *cell = nil;
Model *title = nil;
title = [self.titleList get:indexPath.row];
if (cell == NULL)
{
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell movieTitleLabel].text = [title get:@"title"];
[[cell movieImageView] setImageWithURL: thumbnail];
}
return cell;
}
【问题讨论】:
-
为什么要在 cellForRowAtIndexPath 方法中设置表委托和数据源?如果一开始就没有设置数据源,它将永远不会被调用
-
我应该在哪里设置?我试着把它放在 viewDidApper
-
重新加载数据之前的任何位置。 viewDidLoad: 是通常的地方。或者如果您使用 IB - 最好在那里设置委托和数据源
标签: ios objective-c xcode uitableview reloaddata