简体   繁体   English

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

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

I am using UITableView in my application and I have created custom cell in the file DealsCC.xib and when cell is tapped the color of the cell should be changed to blue but it does not happen in my code. 我在应用程序中使用UITableView,并且已在文件DealsCC.xib中创建了自定义单元格,当点击了单元格时,单元格的颜色应更改为蓝色,但在我的代码中不会发生。 I write the code as follows: 我将代码编写如下:

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

I want to mention that in the line 我想在一行中提到

[cell selectionStyle:UITableViewCellSelectionStyleBlue];

warning msg exists and it is "dealsCC may not respond to -selectionStyle" 警告消息存在,并且为“ dealsCC可能未响应-selectionStyle”

plz help me to solve this problem thanx in advance. 请帮助我提前解决这个问题。

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

Try this in your custom class for the table view cell 在表格视图单元格的自定义类中尝试此操作

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

}

The warning is because the method should be 警告是因为该方法应该是

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

and not [cell selectionStyle:UITableViewCellSelectionStyleBlue]; 而不是[cell selectionStyle:UITableViewCellSelectionStyleBlue];

Use cell.selectionStyle = UITableViewCellSelectionStyleGray; 使用cell.selectionStyle = UITableViewCellSelectionStyleGray; for changing the style of the cell selection. 用于更改单元格选择的样式。 But here the selectionStyle only takes input of UITableViewCellSelectionStyle type. 但是在这里, selectionStyle只接受UITableViewCellSelectionStyle类型的输入。

For changing Color you need to use Image. 要更改颜色,您需要使用图像。 Use selectedBackgroundView.image property and Follow the tutorial Link 使用selectedBackgroundView.image属性并遵循教程链接

Or you can also try 或者您也可以尝试

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

EDIT: 编辑:

OK I am updating code in case you have any other controls on the cell, then you can try below code. 好的,如果单元格上还有其他控件,我将更新代码,然后可以尝试以下代码。

// 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