首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何更改UITableViewCell的蓝色突出显示颜色?

如何更改UITableViewCell的蓝色突出显示颜色?
EN

Stack Overflow用户
提问于 2010-03-31 22:25:28
回答 7查看 125.3K关注 0票数 137

我想知道如何更改UITableViewCell的蓝色突出显示/选择颜色,有什么想法吗?

EN

回答 7

Stack Overflow用户

发布于 2016-07-27 02:57:49

在Swift中,在cellForRowAtIndexPath中使用它

代码语言:javascript
复制
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView

如果希望选择的颜色在每个UITableViewCell中都相同,请在AppDelegate中使用此选项。

代码语言:javascript
复制
let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
票数 21
EN

Stack Overflow用户

发布于 2014-09-16 22:38:52

如果您想在整个应用程序范围内进行更改,您可以将逻辑添加到您的app Delegate中

代码语言:javascript
复制
class AppDelegate: UIResponder, UIApplicationDelegate {

    //... truncated

   func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        // set up your background color view
        let colorView = UIView()
        colorView.backgroundColor = UIColor.yellowColor()

        // use UITableViewCell.appearance() to configure 
        // the default appearance of all UITableViewCells in your app
        UITableViewCell.appearance().selectedBackgroundView = colorView

        return true
    }

    //... truncated
}
票数 18
EN

Stack Overflow用户

发布于 2015-06-15 02:22:07

为了完整性:如果您创建了自己的UITableViewCell子类,则可以实现- (void)setSelected:(BOOL)selected animated:(BOOL)animated方法,并设置在content视图中添加的某些视图的背景色。(如果是这样的话)或contentView本身(如果您自己的某个视图没有涉及到它)。

代码语言:javascript
复制
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected) {
        self.contentView.backgroundColor = UIColor.blueColor;
    } else {
        self.contentView.backgroundColor = UIColor.whiteColor;
    }
}

(没有用过?为了适应较小宽度的源代码DIV :)

这种方法比使用selectedBackgroundView有两个优点,它使用更少的内存和更少的CPU,除非你显示数百个单元格,否则你甚至不会注意到。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2553746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档