为什么[self.tableView reloadData]不起作用?

时间:2011-03-18 07:26:49

标签: iphone objective-c ios uitableview

这是cellForRowAtIndexPath:方法的代码。

- (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];时没有发生任何事情?

3 个答案:

答案 0 :(得分:10)

您需要至少实现tableView:numberOfRowsInSection:方法作为UITableViewDataSource协议的一部分,并tableView:cellForRowAtIndexPath:(您已经做过)。您还应该实施numberOfSectionsInTableView:

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

self.tableView.dataSource = self;

这需要通过Interface Builder或awakeFromNib或其他方法(如viewDidLoad:)完成。

答案 1 :(得分:1)

你检查你的数据源和UITableView的委托吗?我认为dataSource是零或其他。

答案 2 :(得分:1)

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