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

标签 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]; 什么都没发生?

最佳答案

您至少需要实现 tableView:numberOfRowsInSection:方法作为 UITableViewDataSource 协议(protocol)的一部分,以及 tableView:cellForRowAtIndexPath:(您已经这样做了)。您还应该实现 numberOfSectionsInTableView:

然后你需要确保你的类确实被用作数据源:

self.tableView.dataSource = self;

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

关于iphone - 为什么 [self.tableView reloadData] 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5349162/

相关文章:

ios - 在 customCell 中循环 uiimageview

iphone - NSLocalizedString "localizedStringForKey:value:table:"

iphone - 查找方向改变后元素的位置

iphone - MPNowPlayingInfoCenter 与 AVAudioPlayer 兼容吗?

iphone - 最大限度地减少本地化和 iPhone 4/5 的 xibs 多样性

ios - UIViewController 存在

ios - UITableView 自定义单元格中单元格重用的问题

ios - UITextView 光标/滚动移动问题

iphone - UITableView 单元格内存泄漏

iphone - 动画-不断缩小的图像比例