2013-08-30 155 views
1

我有一个类MTTableViewCell:UITableViewCell 该类中的init方法如下: 请注意,我将backgroundcolor设置为紫色。自定义TableView单元格未配置

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

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self) 
    { 
    [self setBackgroundColor:[UIColor purpleColor]]; 
    } 
    return self; 

    // return [self initMVTableViewCellWithStyle:style reuseIdentifier:reuseIdentifier cellColor:nil]; 
} 

我打电话从实现代码如下

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     static NSString* identifier = @"one"; 
     [self.tableView registerClass:[MVTTableViewCell class] forCellReuseIdentifier:identifier]; 

     MVTTableViewCell *cell = [[MVTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

     cell.textLabel.text = [self->datasource objectAtIndex:[indexPath row]]; 
     return cell; 



    } 

但是我没有看到在表视图中单元格的颜色的任何变化的委托下面的方法。 什么问题?

回答

1

我会尝试移动你的调用注册类到你的viewDidLoad方法,而不是alloc/initing单元,尝试从表中取出一个。通过注册单元的类来重用,您正准备将其用于表格的回收。注:请确保您注册小区ID是一样的,你在的cellForRowAtIndexPath访问一个:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView registerClass:[MVTTableViewCell class] forCellReuseIdentifier:@"one"]; 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"one"; 

    MVTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    cell.textLabel.text = [self->datasource objectAtIndex:[indexPath row]]; 
    return cell; 
} 

不幸的是,我不是机器,我可以测试这个的我似乎无法在请记住在这种情况下的细胞调用结构(它迟了:))但我会检查是否可能调用了不同的init,否则,请尝试在cellFor ...中设置背景颜色以排除故障。尝试在单元格的contentView上设置它。

+0

尝试它。没有工作。我正在使用alloc/init作为测试的一种方式。出院也没有工作。还有其他建议吗? – newbie

+0

以下代码修复了它:cell.textLabel.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = [UIColor purpleColor];所以我猜这个文本标签的白色是阻止contentView的紫色。 – newbie

1

当我想改变我的单元格的背景颜色,我通常使用:

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

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self) 
    { 
    [self.contentView setBackgroundColor:[UIColor purpleColor]]; 
    } 
    return self; 


} 
相关问题