在UITableView中的复选标记上更改颜色

问题描述 投票:54回答:15

有人可以如此友好地告诉我如何更改UITableView中的复选标记上的颜色吗?

我已经搜索但似乎没有让它工作。

干杯

objective-c uitableview uicolor
15个回答
42
投票

Apple没有提供更改复选标记颜色的公共方法,因此您必须使用图像进行更改。

这很简单,只需将单元格的accesoryView属性设置为包含正确颜色复选标记的UIImageView。

它看起来像这样:

UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
cell.accessoryView = checkmark;
[checkmark release];

请享用!


2
投票
#import "UIImage+Color.h"

UIImage *image = [[UIImage imageNamed:@"ic_check_black_24dp.png"] changeColor:CLR_BUY];
UIImageView *checkmark = [[UIImageView alloc] initWithImage:image];
cell.accessoryView = checkmark;

2
投票
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HNCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HNCustomTableViewCell" forIndexPath:indexPath];
cell.tintColor = [UIColor colorWithRed:0.99 green:0.74 blue:0.10 alpha:1.0];
return cell;
}

它对我有用。


2
投票

我总是用这种简单的方法:

UITableViewCell *cell = ...;
cell.tintColor = [UIColor greenColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

1
投票

您不必使用自己的图像,只需在视图中更改它,并使用以下代码加载。

[self.tableView setTintColor:[UIColor colorWithRed:250/255.0 green:223/255.0 blue:6/255.0 alpha:1]]

干杯!


1
投票

以上答案都很棒。但如果你想在全球范围内做,那就去做吧

UITableViewCell.appearance().tintColor = .green

不错的小技巧:)


0
投票

Swift 3.1,iOS 9+

if #available(iOS 9.0, *) {
            UIImageView.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).tintColor = UIColor.themeTint //add your color here 
        } else {
            // Fallback on earlier versions
        }


200
投票

由于iOS SDK自已接受的答案后发生了变化,我想我只是用新的答案进行更新。

事实上你可以通过调整UITableViewCelltintColor属性来改变UITableViewCell.中的复选标记的颜色

您还可以为所有UITableViewCell设置外观代理,以便所有实例都具有特定的色调颜色,除非另有说明

[[UITableViewCell appearance] setTintColor:[UIColor redColor]];

31
投票

如果您正在寻找Swift版本:

Directly on the cell

例如在tableView(_:,cellForRowAtIndexPath:)

cell.tintColor = UIColor.redColor()

Using the appearance protocol

UITableViewCell.appearance().tintColor = UIColor.redColor()

29
投票

以下在iOS 7中为我工作。

[self.tableView setTintColor:[UIColor someColor]];

14
投票

此图显示了如何在故事板中执行此操作。色调颜色是复选标记颜色。


13
投票

我发现igraczech的答案大多是正确的,但是对于iOS 6或更高版本,你可以设置整个tableview的色调颜色,默认项目将继承。

[self.tableView setTintColor:[UIColor someColor]];

这对我有用,并允许我在复选标记中着色。


8
投票

从iOS 7开始,您可以设置视图控制器视图的色调颜色,以便将此色调颜色提升到其所有子视图。因此,要将UITableViewCell的复选标记设置为紫色(例如),在viewWillAppear方法中,您需要:

[self.view setTintColor:[UIColor purpleColor]];

4
投票
 UITableViewCell *cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
cell.tintColor=UIColor.whiteColor;
return cell;

3
投票

UIAccessoryTypeCheckmark(右侧)继承其tableview的背景颜色。

self.tableView.backgroundColor = [UIColor clearColor];
© www.soinside.com 2019 - 2024. All rights reserved.