简体   繁体   English

如何将单元格的背景色设置为有色黑色

[英]How to set the background color of a cell to a tinted black color

I want to set the color of the cell to a transparent black. 我想将cellcolor设置为透明的黑色。 How can I do that? 我怎样才能做到这一点?

I tried to set the alpha to .5 but it doesn't work. 我试图将alpha设置为.5,但是它不起作用。

cell.backgroundColor = [UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 0.5];

On the UITableViewCell Class Reference , there is the following note: UITableViewCell类参考上 ,有以下注意事项:

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView ) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. 注意:如果要更改单元格的背景色(通过通过UIView声明的backgroundColor属性设置单元格的背景色),则必须在委托的tableView:willDisplayCell:forRowAtIndexPath:方法中而不是在tableView:cellForRowAtIndexPath: tableView:willDisplayCell:forRowAtIndexPath: tableView:cellForRowAtIndexPath:数据源的tableView:cellForRowAtIndexPath: Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. 在组样式表视图中更改单元格的背景色在iOS 3.0中具有与以前版本的操作系统不同的效果。 It now affects the area inside the rounded rectangle instead of the area outside of it. 现在,它将影响圆角矩形内部的区域,而不是其外部的区域。

So, put the code cell.backgroundColor = [UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 0.5]; 因此,将代码cell.backgroundColor = [UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 0.5]; on the right place and it should work as you want. 在正确的地方,它应该可以根据需要工作。

Another option is to set a background view for you cell, with the selected color or gradient. 另一个选项是使用选定的颜色或渐变为您的单元格设置背景视图。

set the cell background color in ' willDisplayCell ' delegate method of the tableView. 在tableView的' willDisplayCell '委托方法中设置单元格背景色。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
                                               forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 0.5]];
}

It will work now. 现在可以使用了。 Check It. 核实。

cell.background-color: transparent; with the page background-color set to black? 页面background-color设置为黑色?

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

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