【问题标题】:change the color of the cell when it is tapped in UITableView在 UITableView 中点击时更改单元格的颜色
【发布时间】:2011-11-10 04:27:57
【问题描述】:

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

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

我想在一行中提到这一点

[cell selectionStyle:UITableViewCellSelectionStyleBlue];

存在警告消息,它是“dealsCC 可能无法响应 -selectionStyle”

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

【问题讨论】:

    标签: iphone objective-c xcode uitableview


    【解决方案1】:

    请查看以下stackoverflow链接,请检查您是否已正确关注它们 1)link1 2)link2 3)enter link description here

    【讨论】:

      【解决方案2】:

      在表格视图单元格的自定义类中试试这个

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

      警告是因为方法应该是

      [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
      

      而不是[cell selectionStyle:UITableViewCellSelectionStyleBlue];

      【讨论】:

        【解决方案3】:

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

        要更改颜色,您需要使用 Image。使用selectedBackgroundView.image 属性并关注tutorial Link

        或者你也可以试试

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

        【讨论】:

        • 你尝试了哪一个。我给了你3种方法。第二个选项肯定会起作用。您是否在 Cell 上使用不同的图像。然后你需要在 SelectionStyle 时更改它...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2019-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多