使用 Storyboard 中的 UITableViewCell 时使用 alloc 和 init [英] Using alloc and init when using a UITableViewCell from Storyboard

查看:26
本文介绍了使用 Storyboard 中的 UITableViewCell 时使用 alloc 和 init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Storyboard 做一个简单的应用程序,它有一个带有 UITableView 的视图和一个 UITableViewCell ,可以导航到另一个 UIView>.

I'm doing a simples app using Storyboard that a have a View with a UITableView with a UITableViewCell that do the navigation to another UIView.

所以必须编写代码来填充表格视图上的单元格.

So a have to code to populate the cell on the table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SampleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        NSLog(@"cai no init da cell");
    }

    GPItem *item = [self.items objectAtIndex:indexPath.row];

    cell.textLabel.text = @"Post";
    cell.detailTextLabel.text = item.imageURL;

    return cell;
}

我意识到代码 if (cell == nil) { ... 永远不会执行,所以我真的需要在使用 Storyboard 的单元格时这样做?

I realised that the code if (cell == nil) { ... never executes so I really need to do that on uses the cell from Storyboard?

谢谢.

推荐答案

你说得对;如果您使用故事板,该代码保证返回非零单元格.此外,在 iOS 6 中,新调用 dequeueReusableCellWithIdentifier:forIndexPath: never 返回 nil.请参阅我书中的讨论:

You are correct; that code is guaranteed to return a non-nil cell if you are using a storyboard. Also, in iOS 6, the new call dequeueReusableCellWithIdentifier:forIndexPath: never returns nil. See the discussion in my book:

http://www.apeth.com/iOSBook/ch21.html#_registering_a_cell_class

这篇关于使用 Storyboard 中的 UITableViewCell 时使用 alloc 和 init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆