繁体   English   中英

在UITableView中点击时更改单元格的颜色

[英]change the color of the cell when it is tapped in UITableView

我在应用程序中使用UITableView,并且已在文件DealsCC.xib中创建了自定义单元格,当点击了单元格时,单元格的颜色应更改为蓝色,但在我的代码中不会发生。 我将代码编写如下:

static NSString *MyIdentifier = @"dealsCC";
    dealsCC *cell = (dealsCC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    [cell selectionStyle:UITableViewCellSelectionStyleBlue];

我想在一行中提到

[cell selectionStyle:UITableViewCellSelectionStyleBlue];

警告消息存在,并且为“ dealsCC可能未响应-selectionStyle”

请帮助我提前解决这个问题。

请参阅以下链接计算器,请检查您是否遵循correclty他们1) 链接1 2) 链接2 3) 输入链接的描述在这里

在表格视图单元格的自定义类中尝试此操作

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
       self.selectionStyle = UITableViewCellSelectionStyleBlue;
       [super setSelected:selected animated:animated];

}

警告是因为该方法应该是

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

而不是[cell selectionStyle:UITableViewCellSelectionStyleBlue];

使用cell.selectionStyle = UITableViewCellSelectionStyleGray; 用于更改单元格选择的样式。 但是在这里, selectionStyle只接受UITableViewCellSelectionStyle类型的输入。

要更改颜色,您需要使用图像。 使用selectedBackgroundView.image属性并遵循教程链接

或者您也可以尝试

    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor redColor];

编辑:

好的,如果单元格上还有其他控件,我将更新代码,然后可以尝试以下代码。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"myCellId";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     UIView *v = [[[UIView alloc] init] autorelease];
     v.backgroundColor = [UIColor redColor]; // any color of your choice.
     cell.selectedBackgroundView = v;
      // After this line add all other controlls you are adding...
     }
// Set up the cell...
cell.textLabel.text = @"foo";
return cell;
}

暂无
暂无

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

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