繁体   English   中英

每个单元格的背景图像

[英]Background image for each cell

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

return cell之前调用cell.layoutIfNeeded()

暂无
暂无

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

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