【问题标题】:How to make custom TableViewCell with initWithStyle after 3.0如何在 3.0 之后使用 initWithStyle 制作自定义 TableViewCell
【发布时间】:2009-11-18 06:24:41
【问题描述】:

我正在尝试使用 initWithStyle 自定义 TableViewCell,因为它说 initWithFrame 在 3.0 之后已弃用。之前使用 initWithFrame 一切正常。

是否有任何可用的教程或示例代码?谢谢。

【问题讨论】:

  • 我也想知道这个。如果只有一种没有任何隐含视图的样式......

标签: iphone iphone-sdk-3.0 uitableview initwithstyle


【解决方案1】:

我将 UITableViewCell 子类化,然后覆盖 initWithStyle 方法。

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

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        // Initialization code
        msgText = [[UILabel alloc] init];
        [self.contentView addSubview:msgText];  
    }
    return self;
}

msgText 是该类的 UILabel 属性,我在其他地方设置了标签的文本属性。您可以将任何视图添加到您喜欢的 self.contentView。当我添加文本和/或图像等内容时,我还设置了每个子视图的框架。

【讨论】:

  • 你还能在 layoutSubviews 中定义元素的位置吗? tnx
  • 绝对。相对于 UITableViewCell 的左上角设置要添加到 UITableViewCell 子视图的元素的框架。
  • 我没有调用 initWithStyle 方法。你在tableView的cellForRowAtIndexPath中做了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2020-02-19
相关资源
最近更新 更多