ios UITableView 设置背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24743184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView set background color
提问by Sp0tlight
I change the background color of the UITableViewCells in the tableView:cellForRowAtIndexPathmethod
我在tableView:cellForRowAtIndexPath方法中更改了 UITableViewCells 的背景颜色
if(indexPath.row % 2 == 0){
cell.backgroundColor = ...
} else{
cell.backgroundColor = ...
}
But that only change the color of the cell amount specified in tableView:numberOfRowsInSection(As seen in the attached picture, there are white cells after the first four)
但这只会改变tableView:numberOfRowsInSection中指定的单元格数量的颜色(如附图所示,前四个后有白色单元格)
Is it possible to change the color of all cells that are displayed on screen?
是否可以更改屏幕上显示的所有单元格的颜色?
采纳答案by user3386109
If you want the cell background color to continue to alternate, then you need to lie about how many rows are in the table. Specifically, in tableView:numberOfRowsInSectionyou need to always return a number that will fill the screen, and in tableView:cellForRowAtIndexPath, return a blank cell for rows that are beyond the end of the table. The following code demonstrates how to do this, assuming that self.dataArrayis an NSArray of NSStrings.
如果您希望单元格背景颜色继续交替,那么您需要谎报表格中有多少行。具体来说,在tableView:numberOfRowsInSection 中,您需要始终返回一个将填满屏幕的数字,而在tableView:cellForRowAtIndexPath 中,为超出表格末尾的行返回一个空白单元格。以下代码演示了如何执行此操作,假设self.dataArray是 NSStrings 的 NSArray。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( self.dataArray.count < 10 )
return( 10 );
else
return( self.dataArray.count );
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell"];
if ( indexPath.row % 2 == 0 )
cell.backgroundColor = [UIColor orangeColor];
else
cell.backgroundColor = [UIColor redColor];
if ( indexPath.row < self.dataArray.count )
cell.textLabel.text = self.dataArray[indexPath.row];
else
cell.textLabel.text = nil;
return cell;
}
回答by Keenle
- Open Storyboard
- Select your UITableView
- Open Attribute inspector
- Scroll to View group
- Select background color for entire table.
- 打开故事板
- 选择你的 UITableView
- 打开属性检查器
- 滚动到查看组
- 为整个表格选择背景颜色。
回答by Hussain Shabbir
Try like this:-
像这样尝试:-
self.tableView.backgroundView.backgroundColor = [UIColor blueColor];
回答by Irshad Qureshi
I used this to color alternate cell in a table view
我用它来为表格视图中的备用单元格着色
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.grayColor()
}
else
{
cell.backgroundColor = UIColor.whiteColor()
}
}
回答by ethemsulan
In swift, you can change tableview background color or you can set image to tableview background color like this;
在 swift 中,您可以更改 tableview 背景颜色,也可以像这样将图像设置为 tableview 背景颜色;
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
self.tableView.backgroundColor = UIColor.clearColor()
}
// change cell text color and background color
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.clearColor()
}
回答by A.J. Hernandez
For SWIFT
对于 SWIFT
Thanks to @Irshad Qureshi I was able to get alternating background colors for my prototype cells by adding the following in my cellForRowAtIndexPath:
感谢@Irshad Qureshi,通过在我的 cellForRowAtIndexPath 中添加以下内容,我能够为我的原型单元格获得交替的背景颜色:
if (indexPath.row % 2 == 0)
{
cell!.backgroundColor = UIColor.groupTableViewBackgroundColor()
}
else
{
cell!.backgroundColor = UIColor.whiteColor()
}
回答by NRitH
You need to set the tableview's backgroundView
to nil
and its backgroundColor
to the desired color.
您需要设置的tableview是backgroundView
到nil
其backgroundColor
所需颜色。
回答by Nikolay DS
Swift 5 and up
Swift 5 及以上
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//Set Clear Backcolor
//cell.backgroundColor = .clear
//or in your case
if ( indexPath.row % 2 == 0 )
cell.backgroundColor = .orange
else
cell.backgroundColor = .red
}