自定义UITableViewCell子类 [英] Customizing an UITableViewCell subclass

查看:110
本文介绍了自定义UITableViewCell子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义 UITableViewCell ,它应该具有与默认实现不同的外观。为此,我将 UITableViewCell 子类化,并希望添加标签,文本框和背景图像。似乎只显示背景图像。
也许我完全走错了轨道并且可能继承 UITableViewCell 毕竟是一个坏主意,是否有任何理由为什么会这样,有没有更好的方法?

I would want to create a custom UITableViewCell which should have a different appearance than the default implementation. For this I subclassed the UITableViewCell and want to add a label, textbox and a background image. Only the background image seems not to appear. Maybe I'm completely on the wrong track here and maybe subclassing a UITableViewCell is a bad idea after all, is there any reason why that would be the case and is there a better way?

无论如何这是我尝试的,在子类的 initWithStyle 我把以下内容:

Anyhow this is what I tried, in the subclass's initWithStyle I put the following:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self == nil)
    { 
        return nil;
    }

     UIImage *rowBackground;

     backRowImage = [UIImage imageNamed:@"backRow.png"];
     ((UIImageView *)self.backgroundView).image = backRowImage;

 }

我在这里做错了什么?我是否应该在 drawRect 方法中设置背景图像?

What am I doing wrong here? Should I set the background image in the drawRect method as well?

推荐答案

根据头文件,对于普通样式的表,backgroundView默认为nil。您应该尝试创建自己的UIImageView并将其粘贴在那里。

According to the header file, the backgroundView defaults to nil for plain-style tables. You should try creating your own UIImageView and sticking it in there.

这篇关于自定义UITableViewCell子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆