用于自定义的UITableView在tableviewCells故事板创建编程[关闭](custom

2019-08-17 18:54发布

在我的应用程序,我用故事板。 但是,我创建了一个UITableView程序,而不是从对象库拖拽。 现在我想自定义程序创建的细胞UITableView 。 谁能帮我提供创建的一个例子UITableViewCell在故事板编程?

Answer 1:

我尽量把你的电池的布局和建设纳入cellForRowAtIndexPath

要创建一个自定义单元格编程,你应该先创建一个UITableViewCell子类。

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

编程

- (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"];

与界面生成器

尽管如此创建自定义的子类,还创建了同名的厦门国际银行文件。 然后在您的厦门国际银行文件,你可以挂钩的网点,而不必在细胞的初始化创建它们。 (如果你做这种方式,随后的初始化不会反正叫)。

你唯一需要的其他变化是,在viewDidLoad ,你需要注册为细胞而不是类的笔尖。

像这样...

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

然后,一切的工作原理相同。

使用Cell

要使用您所创建的子类细胞...

- (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元素。



Answer 2:

我将介绍两种选择:通过使用Interface Builder(简单)添加单元格,并以编程方式添加单元格:

通过使用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;
}

有在苹果的网站了解更多信息: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html



文章来源: custom tableviewCells for UITableView created programatically in storyboard [closed]