简体   繁体   English

静态 UITableView 中的自定义 UITableViewCell

[英]Custom UITableViewCell in static UITableView

I've defined a custom UITableViewCell that I'm using in my static UITableView .我已经定义了一个自定义UITableViewCell ,我在我的静态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.我想访问我定义的变量( myTableViewImageCell等),但这样做tableView:willDisplayCell似乎是说一起的“细胞类型的重新定义线的东西。

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: .但是请注意,如果您对所有单元格执行相同的操作(如您在此处显示的那样),您应该在cellForRowAtIndexPath:而不是willDisplayCell:执行此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]];      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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