我现在很迷茫,不知道问题可能在哪里。

我创建了一个单视图应用程序(使用ARC和Storyboard),在主视图中添加了一个表视图,将其委托和数据源设置到我的viewcontroller中并实现了

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.games count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = [self.games objectAtIndex:[indexPath row]];

    return cell;
}


作为协议方法。

self.games是带有某些NSString的数组。
当我构建并运行该应用程序时,将显示一个空的TableView。

我究竟做错了什么?

最佳答案

您是否正在为tableView设置委托源和数据源?

您需要使您的类符合UITableViewDelegateUITableViewDataSource协议。 (cellForRowAtIndexPath:UITableViewDataSource协议中)

为此,请在类接口定义(即.h文件中)中使用尖括号:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}


然后在您的.m文件中,您需要设置tableView的委托文件所有者以及dataSource。这样完成-

[tableView setDelegate:self];
[tableView setDataSource:self];


试一下。

关于ios - UITableView将不显示数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8602151/

10-12 22:01
查看更多