iphone - UITableViewController不会显示创建时的单元格

标签 iphone ios uitableview

首先,我是 iOS 开发新手,所以这可能是一个简单的问题,

我有以下代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

    cell.textLabel.text = @"Hello!";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSLog(@"Creating cell for %i:%i", indexPath.section, indexPath.row );

    return cell;
}

现在表格已显示,但所有行都是空白。细胞正在被创建。不确定这是否重要,但我不使用 xib 或 Storyboard,所以我不知道这是否是样式问题。

我对自己在做什么真的一无所知。我尝试遵循一些教程,但每次我都会在同一个地方看到一张空白表格。

感谢大家的帮助!我对想法和可能的教程等持开放态度。

最佳答案

(这更像是一条评论,但对于评论字段来说太大了。)

我已将您的方法准确复制到我的 .m 文件中,并且它有效。文件的其余部分如下所示:

@interface MYViewController ()

@end

@implementation MYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 640) style:UITableViewStylePlain];
    [table setDataSource:self];
    [table setDelegate:self];
    [self.view addSubview:table];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

我假设您设置了数据源,因为您要打印 NSLogs。

关于iphone - UITableViewController不会显示创建时的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993707/

相关文章:

ios - 在 UITableView 之上从 UIView 传递触摸事件

ios - uiscrollview 中的 UIViewControllers 调用 viewdidappear

ios - PayUMoney 支付网关问题

ios - currentPlaybackTime 和 MPMusicPlayerController 以及 iOS 7.1 的问题

iphone - GCD 的后进先出堆栈?

uitableview - UIView-Encapsulated-Layout-Width/Height 约束为零

ios - 为什么单元格高度为零

iphone - 使用通配符配置文件有什么优势吗?

ios - 查找应用已设置的本地通知列表

objective-c - 将 NSNumber 值与定义的 int 进行比较