9

在我的应用程序中,我正在使用情节提要。但是我以编程方式创建了一个UITableView,而不是从对象库中拖动。现在我想自定义以编程方式创建的单元格UITableViewUITableViewCell任何人都可以通过提供在情节提要中以编程方式创建的示例来帮助我吗?

4

2 回答 2

8

我会避免将单元格的布局和构建放入cellForRowAtIndexPath.

要以编程方式创建自定义单元,您应该首先创建一个UITableViewCell子类。

添加到它labelsimageViews等...添加作为子视图cell.contentView

以编程方式

IE

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 21)];
        [self.contentView addSubview:_label];
    }
    return self;
}

如果你想做单元格的布局,那么在MyCell课堂上你可以做......

- (void)layoutSubViews
{
    [super layoutSubviews];
    // layout stuff relative to the size of the cell.
}

然后在tableViewController你需要注册单元类...

viewDidLoad...

[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCellIdentifier"];

与界面构建器

仍然创建自定义子类,但还要创建一个同名的 xib 文件。然后在您的 xib 文件中,您可以连接插座,而不必在单元格的 init 中创建它们。(如果你这样做,那么 init 无论如何都不会被调用)。

您需要的唯一其他更改是viewDidLoad您需要为单元格而不是类注册笔尖。

像这样...

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCellIdentifier"];

然后其他一切都一样。

使用细胞

要使用您为其创建子类的单元格...

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

    [self configureCustomCell:(MyCell*)cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCustomCell:(MyCell*)cell atIndexPath:(NSIndexPath *)indexPath
{
    // do all you logic of getting any info from arrays etc in here.

    cell.label.text = @"Blah".
}

概括

这样做意味着您的 tableviewcontroller 只对将内容放入单元格感兴趣。如果您将所有逻辑都用于构建细胞,那么一切都会变得非常混乱。

这也意味着您不必处理大量不同的标签来保存和检索不同的 UI 元素。

于 2013-03-20T08:28:47.603 回答
0

我将描述两个选项:使用 Interface Builder(更简单)添加单元格,以及以编程方式添加单元格:

通过使用界面生成器

在 Interface Builder 中创建单元格并使用自定义内容添加子视图后,打开属性检查器,选择自定义样式并在重用标识符文本字段中输入唯一标识符(例如,“anIdentifier”)。接下来,选择您要以编程方式访问的单元格字段,并为每个字段设置一个唯一的标记号(位于“查看”部分下)。

然后,在您的数据源代码中,实现此方法:

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

    UILabel *label;

    label = (UILabel *)[cell viewWithTag:1]; // Set a constant for this so your fellow developers understand this number
    label.text = @"This is a test";

    return cell;
}

以编程方式

如果您想以编程方式创建单元格,则代码应类似于以下内容:

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

    static NSString *CellIdentifier = @"anIdentifier";

    UILabel *aLabel;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]];

        aLabel = [[[UILabel alloc] initWithFrame:...]];
        aLabel.tag = 1; // Set a constant for this
        aLabel.font = [UIFont systemFontOfSize:14.0];
        aLabel.textAlignment = UITextAlignmentRight;
        aLabel.textColor = [UIColor blackColor];
        aLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];
    } else {
        aLabel = (UILabel *)[cell.contentView viewWithTag:1];
    }

    aLabel.text = @"This is a test";

    return cell;
}

Apple 网站上有更多信息:http: //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

于 2013-03-20T07:51:38.280 回答