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

时间:2022-11-21 21:20:56

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. I write the code as follows:

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

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.

PLZ帮我提前解决了这个问题。

3 个解决方案

#1


1  

please see the following * links,please check whether you have followed correclty them 1)link1 2)link2 3)enter link description here

请看下面的*链接,请检查你是否已经关注他们1)link1 2)link2 3)在这里输入链接描述

#2


1  

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];

#3


0  

Use cell.selectionStyle = UITableViewCellSelectionStyleGray; for changing the style of the cell selection. But here the selectionStyle only takes input of UITableViewCellSelectionStyle type.

使用cell.selectionStyle = UITableViewCellSelectionStyleGray;用于改变单元格选择的样式。但是这里的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;
}

#1


1  

please see the following * links,please check whether you have followed correclty them 1)link1 2)link2 3)enter link description here

请看下面的*链接,请检查你是否已经关注他们1)link1 2)link2 3)在这里输入链接描述

#2


1  

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];

#3


0  

Use cell.selectionStyle = UITableViewCellSelectionStyleGray; for changing the style of the cell selection. But here the selectionStyle only takes input of UITableViewCellSelectionStyle type.

使用cell.selectionStyle = UITableViewCellSelectionStyleGray;用于改变单元格选择的样式。但是这里的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;
}