2

我看过很多例子,我找不到最好的答案。

想象一下,我有一个高度为 120 的自定义单元格,有 3 个 UILabel 和一个 UIImageView。

我用

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

在所有方法中,哪一种是最优化的?

4

3 回答 3

1

你所要做的就是这样的:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];
    UILabel *label;
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"Prototype Cell"];
        label = [[UILabel alloc] init];
        label.tag = LABEL_TAG;  //Suppose LABEL_TAG is defined someplace else
        [cell.contentView addSubview: label];
    }
    else 
    {
        label = [cell.contentView viewWithTag:LABEL_TAG];
    }


    label.text = @"Hello World";

    //Suppose you want the label at x coordinate 50
    label.frame = CGRectMake(0,0,50,120); 

    //Follow similar steps for all of your subviews

    return cell;
}
于 2012-07-17T17:25:05.760 回答
1

创建自定义表的最佳答案可能在下面的链接中,如果您对在那里看到的代码有具体问题,请随时提出更多问题。

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

于 2012-07-17T17:25:14.917 回答
1

通过继承 UITableViewCell 并覆盖方法 initWithStyle 来创建自定义 UITableviewCell。您可以在此 init 方法中分配标签并在此方法中的单元格中添加此标签。

您还可以使用 nib 文件创建 UItableViewCell。

http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/

于 2012-07-17T17:39:34.537 回答