ios - 每个单元格的背景图像

标签 ios swift uitableview

我想创建一个 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/

相关文章:

ios - Mac/iOS 应用程序中中介者模式的实现 (Swift)

iphone - 无法解析 XML iPhone

ios - 以编程方式展开 segue

iphone - 使用 NSMutableSet 时出错

ios - 实例成员不能用于类 NSObject 的类型

ios - 如何在 swift 中以编程方式保存 pdf

ios - UISlider 长按可在 UITableView 中移动它

ios - 如何获取 UICollectionViewCell 的矩形?

ios - 点击一个单元格也会影响其他单元格

ios - 如何在 Xcode 12.4 上运行 iOS 模拟器 10.3.1?