objective-c - TableView 数据在模拟器上不可见

标签 objective-c ios ipad

我正在使用 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但只需添加 UITableViewUIViewController ,您需要将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/

相关文章:

iphone - XCode (iOS) 中委托(delegate)的自动导入回调

iphone - 如果设备是 iPad,如何将方向更改为横向

iphone - iOS 开发 : How can I return data from a modal view?

objective-c - HDFC iOS SDK 崩溃以集成到 Swift 中?

objective-c - 我应该在 dealloc 中释放实例变量和属性吗?

objective-c - NSOutlineView 选择不起作用

android - 如何在 iOS 中使用 EXPO/react-native 实现 RTL

iphone - 我在哪里可以找到有关 IOS 日历同步的好教程?

ios - 在iOS中合成CGFloat属性

objective-c - UIBezierPath 橡皮擦