3.0之后如何使用initWithStyle制作自定义TableViewCell

发布于 2024-08-11 18:32:43 字数 124 浏览 12 评论 0原文

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

有可用的教程或示例代码吗?谢谢。

I am trying to have custom TableViewCell with initWithStyle, since it says initWithFrame is deprecated after 3.0. Everything worked fine with initWithFrame before.

Is there any tutorials or sample code available for this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-08-18 18:32:43

我对 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 中。当我添加文本和/或图像等内容时,我还会设置每个子视图的框架。

I have subclassed UITableViewCell then override the initWithStyle method.

- (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 is a UILabel property of the class and I set the text property of the label elsewhere. You can add any views to self.contentView you like. I also set the frame of each of the subviews when I add the content like text and/or images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文