我正在使用 TableView 来显示数据数组,但是当我编译应用程序时,似乎没有调用 TableView 的函数。以下是我用于此目的的代码:
- (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section {
return listdata.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"myCell"];
if (cell == nil) {
cell = [[ UITableViewCell alloc ] initWithFrame:CGRectZero];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.listdata objectAtIndex:row];
return cell;
}
最佳答案
如果您不想使用UITableViewController
但只需添加 UITableView
在 UIViewController
,您需要将TableView的委托(delegate)和数据源设置为VC的类。
您可以在 Storyboard 中执行此操作,方法是按住 Ctrl 键从 TableView 拖动到 ViewController 并选择委托(delegate)/数据源,或者您可以通过将此代码放入 ViewController 的 .m 中的 viewDidLoad
中以编程方式执行此操作。方法:
_tableView.delegate = self;
_tableView.datasource = self;
并添加 <UITableViewDelegate, UITableViewDatasource>
到你的 ViewController 的 .h :
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
关于objective-c - TableView 数据在模拟器上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804029/