简体   繁体   English

在UITableViewCell的setSelected和setHighlighted方法中创建自定义AccessoriesView导致其余代码被忽略

[英]Creating a custom accessoryView in the UITableViewCell's setSelected and setHighlighted methods is causing the rest of my code to be ignored

In my UITableViewCell subclass, I'm overriding the setHighlighted and setSelected methods to change the look of the cell when it's selected, but whenever I set the accessoryView property in either of the methods, all my other code that changes the font and shadow colors is ignored entirely. 在我的UITableViewCell子类中,我重写setHighlighted和setSelected方法以在选定单元格时更改单元格的外观,但是每当我在两个方法中的任何一个中设置了附件视图属性时,所有其他更改字体和阴影颜色的代码都是完全忽略了。

For example, using this code will change the text color of the text and detail labels 例如,使用此代码将更改文本和详细标签的文本颜色

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {        
        self.textLabel.textColor         = [UIColor whiteColor];
        self.textLabel.shadowColor       = [UIColor clearColor];
        self.detailTextLabel.textColor   = [UIColor whiteColor];
        self.detailTextLabel.shadowColor = [UIColor clearColor];
    } 
}

But the moment I add the custom accessoryView to the mix, all the other code is ignored, however the accessoryView image does appear. 但是,当我将自定义的附件视图添加到混合中时,所有其他代码都将被忽略,但是附件视图图像的确出现了。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {        
        self.textLabel.textColor         = [UIColor whiteColor];
        self.textLabel.shadowColor       = [UIColor clearColor];
        self.detailTextLabel.textColor   = [UIColor whiteColor];
        self.detailTextLabel.shadowColor = [UIColor clearColor];

        self.accessoryView = 
            [[UIImageView alloc] initWithImage:styleImage(@"/icons/disclosure_selected.png")];
    } 
}

Is there something I'm doing wrong? 我做错了什么吗? How can I properly customize the accessoryView and the rest of the cell's code during the selected and highlighted states? 在选定和突出显示的状态下,我如何正确自定义附件视图和单元格的其余代码?

我最终只是创建了自己的UIImageView并隐藏了内置的UIImageView。

I think accessoryView is a UIButton not a UIImageView... Try this : 我认为AccessoriesView是一个UIButton而不是UIImageView ...试试这个:

accessory = [UIButton buttonWithType:UIButtonTypeCustom];
    [accessory setImage:[UIImage imageNamed:@"/icons/disclosure_selected.png"] forState:UIControlStateNormal];
    accessory.frame = CGRectMake(0, 0, 26, 26);      //You can change it
    accessory.userInteractionEnabled = YES;          //You can change it too
    self.accessoryView = accessory;

Hope it'll help 希望对你有帮助

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

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