简体   繁体   中英

Custom UITableViewCell in static UITableView

I've defined a custom UITableViewCell that I'm using in my static UITableView . I'd like to access the variables I've defined ( myTableViewImageCell , etc) but doing it from tableView:willDisplayCell seems to say something along the lines of "redefinition of cell type.

Here's what I'm doing:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    [cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
    [cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];   


}

If all of your cells are the same custom class, you can just change the definition of your method to indicate that and get rid of your first line (which is wrong anyway):

- (void)tableView:(UITableView *)tableView willDisplayCell:(ANMyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    [cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
    [cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
    [cell.myTableViewLabel setTextColor:[UIColor hex666Color]];      
}

Note however, that if you are doing the same thing to all of your cells (as you show here), you should do this in cellForRowAtIndexPath: instead of willDisplayCell: . That way, the code is run only once when the cell is created instead of every time that it is displayed.

FYI, the problem is the extraneous ;cell; you have at the end of the second line. change it to

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    ANMyTableViewCell *cell = (ANMyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    [cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
    [cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
    [cell.myTableViewLabel setTextColor:[UIColor hex666Color]];      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM