简体   繁体   English

自定义单元格中的自定义tableview单元格选择颜色

[英]custom tableview cell selection color in custom cell

I'm already searching for two days about this and can't find my answer... 我已经搜索了两天,找不到答案...

I have a custom uitableviewcell, then I need to implemenent custom color, when cell is selected. 我有一个自定义的uitableviewcell,然后在选择单元格时需要实现自定义颜色。

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];

Doesn't work for me. 对我不起作用。 Or if it works, the background color is clear, and I don't get whats wrong. 或者,如果有效,则背景颜色清晰,而且我没弄错。 It seems that I don't know how to properly connect my custom cell in Interface builder. 似乎我不知道如何在“接口”构建器中正确连接自定义单元。 I can create a custom cell but the only thing is that I don't know how to make the selection work. 我可以创建一个自定义单元格,但是唯一的事情是我不知道如何使选择工作。 So if anyone could help me I would be really thankful. 因此,如果有人可以帮助我,我将非常感激。 Or maybe someone knows a tutorial about this? 也许有人知道有关此的教程?

Thanks in advance! 提前致谢!

Hope the below Code must work. 希望下面的代码必须起作用。 You need to copy paste this code in the customCell implementation file. 您需要将此代码复制粘贴到customCell实现文件中。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

UIView *backgroundView = [[UIView alloc] initWithFrame:self.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor colorWithRed:143/255.f green:141/255.f blue:147/255.f alpha:1.0]];
[self setSelectedBackgroundView:backgroundView];
[backgroundView release];
}

No need for custom cells. 无需自定义单元格。 If you only want to change the selected color of the cell, you can do this: 如果只想更改单元格的选定颜色,则可以执行以下操作:

UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

@Pandey_Laxman is right. @Pandey_Laxman是正确的。 Simply using cell.seletedBackgroundView won't work because either the backgroundView and the selectedBackgroundView of UItableViewCell is nil by default. 简单地使用cell.seletedBackgroundView行不通的,因为cell.seletedBackgroundView的backgroundView和selectedBackgroundView默认为零。 You need to init a UIView before set it as a cell's selectedBackgroundView. 您需要先初始化UIView,然后再将其设置为单元格的selectedBackgroundView。 see the definition of selectedBackgroundView() 请参见selectedBackgroundView()的定义

Finnaly i have found the answer to my question.. 好的,我已经找到了我的问题的答案。

I've made very stupid mistake.. So first thing you need to set the files owner class as NSObject to your customcell in IB, then your uitableviewcell class as your custom cell, in my case "myCustomDisplayCell".. 我犯了一个非常愚蠢的错误。因此,首先需要将文件所有者类设置为NSObject到IB中的customcell,然后将uitableviewcell类设置为定制单元,在我的情况下是“ myCustomDisplayCell”。

Futhermore from bottom to top (views in IB), I've added a uiview and hooked up with cells backgroundview, on top i've placed a uiview and hooked up with cells selectedbackgroundview(made view color clear) then finnaly i placed a uilabel uiimageview, and hooked up these right.. (you can add your own things here needed for custom cells). 此外,从下至上(IB中的视图),我添加了一个uiview并与单元格backgroundview挂钩,在顶部我放置了一个uiview并与单元格selectedbackgroundview挂钩(使视图颜色清晰),然后确定地我放置了uilabel uiimageview,并连接了这些权限。.(您可以在此处添加自定义单元所需的自己的东西)。 And that's it! 就是这样! Looks kind of easy now.. 现在看起来很简单。

Thank you all for help! 谢谢大家的帮助!

You set it in the custom cell class as following (Swift 4): 您可以在自定义单元格类中进行如下设置(快速4):

import UIKit

class CustomTableViewCell: UITableViewCell { 

... 

 override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if(selected) {
            self.backgroundColor = UIColor(named: "BrilliantAzure")
        } else {
            self.backgroundColor = UIColor.white
        }
    }

...

As result, no need to create an additional view for this specific task. 因此,无需为此特定任务创建其他视图。

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

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