简体   繁体   English

Swift Tableview单元格未在部分上显示背景图像

[英]Swift tableview cell not showing background image on sections

Inside cellForRowAtIndexPath method I am atribtuting to the cell a background image like so: 在cellForRowAtIndexPath方法内部,我向单元格添加背景图像,如下所示:

let image = UIImage(named: "other_car")
let imageView = UIImageView(image: image)
cell.backgroundView = imageView

but the cell seems the same. 但单元格似乎相同。 How to correct this? 如何纠正呢?

You're creating an instance of UIImageView without setting its frame. 您正在创建UIImageView的实例而未设置其框架。

Set imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200) for example to set its frame and make it visible. 设置imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)以设置其框架并使其可见。

Also you can set the frame directly while creating it like this: 您也可以在创建框架时直接设置框架,如下所示:

let imageView = UIImageView(frame:CGRectMake(0, 0, 100, 200))

Then you can set the image with: 然后,您可以使用以下方法设置图像:

imageView.image = UIImage(named: "other_car")

or 要么

imageView.image = image // your UIImage instance

What I missed is this: before change the cell.backgroundView = imageView I inserted this peace of code, which cleared the color inside the cell: 我错过的是:在更改cell.backgroundView = imageView之前,我插入了这段和平的代码,这清除了单元格内部的颜色:

cell.backgroundColor = UIColor.clearColor()

Hope it helps other people 希望对别人有帮助

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

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