为 TableView 单元格插入单个背景图像 Swift
Insert a single background image for a TableView Cell Swift
我正在开发一个应用程序以从 JSON 获取数据,然后将结果放在 Table 上,但我的问题是我正在尝试为每个设置图像单元格(相同的图像)但在尝试了几种方法后我无法获得正确的图像。
这是我试过的一些代码:
1.- 在后台
// Add a background view to the table view
let backgroundImage = UIImage(named: "bookshelf1.png")
let imageView = UIImageView(image: backgroundImage)
self.table.backgroundView = imageView
并在单元格中:
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.backgroundColor = UIColor.clear
它显示为图像的扩展图像,而不是每个单元格的单个图像
2.- 在单元格中
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let wifiSpot : NSMutableDictionary = self.tableData[indexPath.row] as! NSMutableDictionary
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.imageView?.image = UIImage(named: "bookshelf1.png")
但是这种方式会与文本重叠,并且还会显示一个白色的列,就像图像没有覆盖整个单元格一样
![第二次尝试]:http://imgur.com/3LPgtwl
3.- 另一种方式
cell.imageView?.image = UIImage(named: "bookshelf1.png")
cell.textLabel?.text = wifiSpot["ssid"] as? String
显示是这样的
![第三次尝试]: http://imgur.com/9zu9ct7
我还尝试在原型单元格上插入一个 ImageView,但只显示单元格 1 的图像。
尝试按住 Ctrl 从原型单元格上的 Imageview 拖动到 CustomTableViewCell,然后像这样创建单元格:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
然后像这样从 CustomTableViewCell 调用图像:
cell.postedImage.image = UIImage(named: "bookshelf1.png")
但它不允许我创建 IBOulet。
因为我的声誉,它不允许我 post 更多图片
感谢您的帮助
尝试将子视图 (cell.imageView
) 发送到其父视图 (cell
) 的后面。
if let imageView = cell.imageView {
cell.sendSubview(toBack: imageView)
}
我想出了怎么做 @Callam 这是我使用的代码,它像我想要的那样工作:
var imageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 100, height: 200))
let image = UIImage(named: "bookshelf1.png")
cell.backgroundColor = UIColor.clear
imageView = UIImageView(image:image)
cell.backgroundView = imageView
// An then the cell content
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.textLabel?.textColor = UIColor.white
这是我想展示的内容:
感谢您的帮助
我正在开发一个应用程序以从 JSON 获取数据,然后将结果放在 Table 上,但我的问题是我正在尝试为每个设置图像单元格(相同的图像)但在尝试了几种方法后我无法获得正确的图像。
这是我试过的一些代码:
1.- 在后台
// Add a background view to the table view
let backgroundImage = UIImage(named: "bookshelf1.png")
let imageView = UIImageView(image: backgroundImage)
self.table.backgroundView = imageView
并在单元格中:
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.backgroundColor = UIColor.clear
它显示为图像的扩展图像,而不是每个单元格的单个图像
2.- 在单元格中
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let wifiSpot : NSMutableDictionary = self.tableData[indexPath.row] as! NSMutableDictionary
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.imageView?.image = UIImage(named: "bookshelf1.png")
但是这种方式会与文本重叠,并且还会显示一个白色的列,就像图像没有覆盖整个单元格一样
![第二次尝试]:http://imgur.com/3LPgtwl
3.- 另一种方式
cell.imageView?.image = UIImage(named: "bookshelf1.png")
cell.textLabel?.text = wifiSpot["ssid"] as? String
显示是这样的
![第三次尝试]: http://imgur.com/9zu9ct7
我还尝试在原型单元格上插入一个 ImageView,但只显示单元格 1 的图像。
尝试按住 Ctrl 从原型单元格上的 Imageview 拖动到 CustomTableViewCell,然后像这样创建单元格:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
然后像这样从 CustomTableViewCell 调用图像:
cell.postedImage.image = UIImage(named: "bookshelf1.png")
但它不允许我创建 IBOulet。
因为我的声誉,它不允许我 post 更多图片
感谢您的帮助
尝试将子视图 (cell.imageView
) 发送到其父视图 (cell
) 的后面。
if let imageView = cell.imageView {
cell.sendSubview(toBack: imageView)
}
我想出了怎么做 @Callam 这是我使用的代码,它像我想要的那样工作:
var imageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 100, height: 200))
let image = UIImage(named: "bookshelf1.png")
cell.backgroundColor = UIColor.clear
imageView = UIImageView(image:image)
cell.backgroundView = imageView
// An then the cell content
cell.textLabel?.text = wifiSpot["ssid"] as? String
cell.textLabel?.textColor = UIColor.white
这是我想展示的内容:
感谢您的帮助