2014-09-23 20 views
0

我想在顯示單元格中做初始設置UITableViewCell但它不起作用。誰能幫我嗎?如何在willDisplayCell方法中配置UITableViewCell?

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.awtxt.font = [UIFont fontWithName:@"Helvetica" size:15.0]; //i am unable to set this .awtxt is the property outlet in my custom ui table view cell named as "postCell" 
} 
+1

您是否嘗試將您的細胞作爲您的自定義單元格?因爲該方法說「單元」是一個「UITableViewCell」。 – 2014-09-23 09:06:25

+0

爲什麼不在'cellForRowAtIndexPath:'或者你的單元子類的'awakeFromNib'中做到這一點? – Andy 2014-09-23 09:59:46

+0

因爲我聽說配置類型的東西應該在顯示方法 – 2014-09-23 10:17:14

回答

4

對我來說,當我將方法從UITableViewCell更改爲我的自定義單元格時,它工作正常。 所以只需用您的自定義postCell

-(void) tableView:(UITableView *)tableView willDisplayCell:(postTableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath // replace "postTableViewCell" with your cell 
{ 

    cell.awtxt.font = [UIFont fontWithName:@"Helvetica" size:15.0]; //i am unable to set this .awtxt is the property outlet in my custom ui table view cell named as "postCell" 

} 
+4

中完成好吧當你有多個自定義原型單元時會發生什麼?你如何迎合他們所有的willDisplayCell方法? – vnchopra 2015-02-09 23:22:44

2

我Mike_NotGuilty同意更換UITableViewCell。我試了一下,他建議的UITableViewCell的我的自定義/子類,稱爲BNRItemCell與方法定義如下,它的偉大工程:

//This function is where all the magic happens 
-(void) tableView:(UITableView *) tableView willDisplayCell:(BNRItemCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Set the cells background color 
    if (indexPath.row % 2 == 0) { 
     UIColor *altCellColor = [UIColor colorWithWhite:0.3 alpha:0.5]; 
     cell.backgroundColor = altCellColor; 
    } 

    [UIView beginAnimations:@"fade" context:nil]; 
    [UIView setAnimationDuration:20.0]; 
    [UIView setAnimationRepeatAutoreverses:YES]; 
    [UIView setAnimationRepeatCount: 0.5]; 
    [cell.nameLabel setAlpha:0.4]; 
    [cell.nameLabel setAlpha:1]; 
    [UIView commitAnimations]; 
} 

您應該與您的自定義一個替換原來的方法定義和嘗試:

-(void) tableView:(UITableView *) tableView willDisplayCell:(PostCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // your code here 
} 
+0

如何在willdisplay方法中使用多個單元格 – 2017-06-14 04:54:15

相關問題