как настроить 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"
}

person warzone_fz    schedule 23.09.2014    source источник
comment
Вы пытались использовать свою ячейку как свою пользовательскую ячейку? потому что метод говорит, что ячейка представляет собой UITableViewCell.   -  person Mike_NotGuilty    schedule 23.09.2014
comment
почему бы вам не сделать это в cellForRowAtIndexPath: или в awakeFromNib подкласса вашей ячейки?   -  person Ben Affleck    schedule 23.09.2014
comment
потому что я слышал, что тип конфигурации должен быть выполнен в методе отображения   -  person warzone_fz    schedule 23.09.2014
comment
Попробуйте это stackoverflow.com/a/37653235/5653015 Примечание: это быстрый ответ   -  person iOS Lifee    schedule 20.04.2018


Ответы (2)


Для меня это сработало, когда я изменил метод с UITableViewCell на свою пользовательскую ячейку. Так что просто замените 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"

 }
person Mike_NotGuilty    schedule 23.09.2014
comment
Хорошо, что происходит, когда у вас есть несколько пользовательских ячеек-прототипов? Как вы обслуживаете все их методы willDisplayCell? - person vnchopra; 10.02.2015

Я согласен с Mike_NotGuilty. Я попробовал то, что он предложил для моего пользовательского/подкласса 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
}
person user3613932    schedule 05.01.2015
comment
как я могу использовать несколько ячеек в методе willdisplay - person Maulik shah; 14.06.2017