0

我对 UITableViewCell 进行了子类化,这样我就可以提高滚动性能,这对我来说效果很好。

在我的子类中,我有一个名为 seSelected 的方法,看起来像这样

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    if (selected) {
        self.backgroundColor = [UIColor lightGrayColor];
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
}

我想知道如何制作它,以便如果我触摸同一个单元格,它会取消选择该单元格并将颜色更改回白色?我在 setSelected 中尝试了一些不同的 if 语句,但没有任何效果。

4

5 回答 5

0

使用 UITableViewCell 方法:[cell setSelectedBackgroundView:myBgColorView];苹果文档。例子:

UIView *myBgColorView = [[UIView alloc] init];
myBgColorView.backgroundColor = [UIColor greenColor];
[cell setSelectedBackgroundView:myBgColorView];
于 2013-10-26T10:32:39.480 回答
0

使用此委托方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

并在此处调用管理选定和未选定单元格的方法。

于 2013-10-26T04:26:04.963 回答
0

一种方法是在单元格上设置标签,然后使用它来设置背景颜色。

typedef enum {
    CellSelected = 0,
    CellDeselected
} CellSelection;

创建单元格时,将标签设置为“CellDeselected”

cell.tag  = CellDeselected;

然后当点击单元格时,只需检查您要在背景中设置的颜色。

switch (customCell.tag) {
    case CellSelected:
        self.backgroundColor = [UIColor lightGrayColor];
        break;
    case CellDeselected:
        self.backgroundColor = [UIColor whiteColor];
        break;
    default:
        break;
}

customCell.tag = !customCell.tag;
于 2013-10-26T04:31:58.850 回答
0

设置单元格样式:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

并且您的代码将起作用。但是,您只设置颜色选定的单元格。如果您需要在按下单元格时设置颜色,请覆盖此方法:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
于 2015-02-09T07:26:25.377 回答
0

这样做...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    str = [YourArray objectAtIndex:indexPath.row];//str is a string variable
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
    }
    cell.textLabel.text = [reminder objectAtIndex:indexPath.row];

    cell.backgroundColor = [UIColor whiteColor];

    if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]])
    {
        cell.backgroundColor = [UIColor grayColor];
    }

    return cell;
}

您可以将其用于您的自定义单元......

于 2013-10-26T04:33:22.413 回答