2011-03-18 57 views
0

这是cellForRowAtIndexPath:方法的代码。为什么[self.tableView reloadData]不起作用?

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 

if (cell == nil) { cell = [[[UITableViewCell alloc] 
    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
} 

NSUInteger row = [indexPath row]; 
cell.textLabel.text = [array objectAtIndex:row]; 
return cell;  
} 

当我使用[self.tableView reloadData];什么都没有发生?

+1

很少Q ?:这个类是UITableViewController的子类吗?在调用[self.tableView reloadData]之前使用新值更新数组; – Hanuman 2011-03-18 07:32:06

+0

不,是我做的.. – mamrezo 2011-03-18 07:39:23

回答

10

您需要至少将tableView:numberOfRowsInSection:方法作为UITableViewDataSource协议的一部分和tableView:cellForRowAtIndexPath:(您已经完成)。您还应该执行numberOfSectionsInTableView:

然后,你需要确保你的类实际上是用作数据源:

self.tableView.dataSource = self; 

需要通过界面生成器来:做或在awakeFromNib或类似viewDidLoad:一些其他的方法。

1

你检查你的dataSource和UITableView的委托吗?我认为dataSource是零或别的东西。

+0

它显示的值 – mamrezo 2011-03-18 07:37:40

+0

嗯,我假设你为UITableView实现dataSource和委托,你确定你的[self.tableView reloadData]被调用了吗?我认为视图出现时会显示某些内容。而且,像Hanuman说的那样,检查你的本地数据,以便在reloadData之前更新dataSource。 – AechoLiu 2011-03-18 08:37:11

+0

我做了所有,当我添加对象viewDidload它显示,但是当我尝试重新加载它不工作 – mamrezo 2011-03-18 09:48:48

1

尝试在行的单元格中放置一个断点并查看是否在您调用reloadData时调用它。同时请检查是否已经对单元格填充的数组进行了更改,并确保表视图连接到其数据源和委托。

相关问题