每个细胞的背景图像

时间:2016-01-31 10:57:26

标签: 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"
]

但我只获得第一个细胞的背景图像。我错过了什么?

2 个答案:

答案 0 :(得分:2)

替换以下代码:

cell.backgroundView = UIView()
cell.backgroundView?.addSubview(imageView)

使用:

cell.backgroundView = imageView;

答案 1 :(得分:0)

cell.layoutIfNeeded()

之前致电return cell