2016-02-17 61 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]; 

希望這有助於!

相關問題