2016-02-17 59 views
1

我有一个自定义UITableViewCell其中我有两个UIViews。我想要更改BackgroundColor半径以及UIViews的一些其他属性。无法更新自定义UITableViewCell中的子视图

但我无法这样做。

这是我的setUp。

步骤1:

Create A Custom Cell with XIB. 

步骤2:在XIB一进Cell IdentiferCustomCell

步骤3:在viewDidLoad

UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; 
[[self mTableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"]; 

步骤4实例化NIB:细胞为行索引方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Create an instance of ItemCell 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 

    [cell updateCellView]; 

    return cell; 
} 

步骤5:两次检查的出口连接都是好的。

第6步:自定义单元格类:

#import "TransportCell.h" 

    @implementation TransportCell 

    - (void)awakeFromNib { 
     // Initialization code 

     } 

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
     [super setSelected:selected animated:animated]; 

     // Configure the view for the selected state 
    } 


    - (void)updateCellView 
    { 
    self.backgroundView.backgroundColor = [UIColor redColor]; 
    } 

    @end 

此代码对我的手机浏览无影响。

我调试了代码。当我登录了backgroundView我得到nilupdateCellView方法被调用:

enter image description here

这里是我的CustomCell的厦门国际银行:

enter image description here

我必须改变内部的UIView属性(蓝色。颜色)

+0

尝试self.backgrondColor = [UIColor redcolor] – riddhi

+0

它会改变单元格背景颜色 – Dalvik

+0

你有没有为你的backgroundview插座或连接到你的视图? – riddhi

回答

0

而不是调用cellForRowAtIndexPath中的方法试试这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Create an instance of ItemCell 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    return cell; 
} 

,然后在您的自定义的方法

- (void)updateCellView 
    { 
    self.backgroundView.backgroundColor = [UIColor redColor]; 

    // reload the table view 
    [self.tableView reloadData]; 
    } 
+1

兄你看到了问题,该方法存在于单元类不在当前视图控制器中 –

+0

我如何获得我的自定义单元格中的表视图的参考 – Dalvik

+0

对不起,我没有检查...但你也可以创建一个在视图控制器中的自定义方法,您可以管理视图 –

0

需要在方法更新单元格视图中添加[实现代码如下reloadData]

+0

updateCellView方法在customClass中。如何将参考传递给该自定义单元 – Dalvik

0

理想的情况下所需要的所有信息,以使细胞应该会来自数据源或其他事件。例如,如果您要更改边框颜色,它应该表示某个实体(如用户)已经执行了某个事件,或者某个值已经在数据源中发生了变化,如超过阈值的值。

在数据源或改变这些事件要么触发刷新整个表:

-(void) reloadData 

或某些细胞:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

只有那么细胞就会得到更新。

+0

我想在单元显示给用户之前更改这些属性。当用户第一次获取我的单元格时,我希望它们出现四舍五入 – Dalvik

+0

重载函数用于动态刷新表格。您是否想根据事件动态更新您的单元格,还是仅在第一次渲染时更新它们? –

0

尝试TransportCell这种方法,这是SWIFT代码做同样的目标C

override func layoutSubviews() 
{ 
self.backgroundView.backgroundColor = [UIColor redColor]; 
} 
0

要更改self.backgroundview.backgroundColor

默认情况下它是UITableViewStylePlainnil的细胞,且非空的。

'backgroundView'将作为所有其他视图背后的子视图添加。

@property (nonatomic, strong, nullable) UIView *backgroundView; 
0

使用此:

self.contentView.backgroundColor = [UIColor redColor]; 

希望这有助于!

相关问题