在UItableviewCell上设置的图像

时间:2014-04-03 06:00:09

标签: ios objective-c uitableview

我有三张图片:正常,突出显示和选中。如何在Tableview自定义单元格中进行设置?

我试过这个:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {
        self.highlighted = NO;
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listselected"]];
    }

} 
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate {
    if (highlighted) {
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listpressed"]];
    }
}

但突出显示仍有很多次。

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试更新您的代码:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {
        self.highlighted = NO;
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listselected"]];
    }
    else{
       ...unselect code
    }

} 
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate {
    if (highlighted) {
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listpressed"]];
    }
    else{
      ...unhighlight
    }
}

答案 1 :(得分:0)

如果您想要didselectrowatindexpath上的所选图像,那么您可以在cellForRowAtIndexPath方法中设置UITableviewCell属性setSelectedBackgroundView,如下所示:

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
{
....
UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];

[normalCellBG setImage:[UIImage imageNamed:@"box_nonselected.png"]];//Set Image for Normal
[normalCellBG setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:normalCellBG];


UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[selecetedCellBG setImage:[UIImage imageNamed:@"box_selected.png"]];//Set Name for selected Cell
[selecetedCellBG setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selecetedCellBG ];

}

如果您需要这样做,请告诉我。