我想创建一个 UITableView
,其中每个单元格都有一个背景图像,到目前为止我正在这样做:
//UITableViewController
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("HomeTVCell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = StaticData.HOME_ITEMS_TEXT[indexPath.row]
cell.textLabel?.backgroundColor = UIColor(white: 1, alpha: 0)
imageView = UIImageView(frame: cell.frame);
imageView.image = UIImage(named: StaticData.HOME_ITEMS_IMGS[indexPath.row])
cell.backgroundView = UIView()
cell.backgroundView?.addSubview(imageView)
NSLog("Row: \(indexPath.row), Img: \(StaticData.HOME_ITEMS_IMGS[indexPath.row])")
return cell
}
StaticData
包含:
static let HOME_ITEMS_TEXT = [
"Get Inspired",
"Places & Attractions",
"Foods",
"Tourist Map",
"Recommended Tours",
"Tips: Save Money",
"Tips: Comfort",
"Hotels",
"Emergency",
"Tourism Companies",
"Travel Companies"
]
static let HOME_ITEMS_IMGS = [
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil",
"AhsanMonzil"
]
但我只获取第一个单元格的背景图像。我错过了什么?
最佳答案
替换下面的代码:
cell.backgroundView = UIView()
cell.backgroundView?.addSubview(imageView)
与:
cell.backgroundView = imageView;
关于ios - 每个单元格的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113031/