2015-08-28 117 views
0

請注意此代碼。Tableviewcell不會更改背景圖片

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentTableViewCell 
    cell.commentText?.text = comments![indexPath.row] 
    if let objectId = yak?["user"].objectId { 
     let commentor = commentorIds[indexPath.row] 
     if commentor == objectId { 
      cell.commentText.textColor = UIColor(red: 136.0/255, green: 135.0/255, blue: 134.0/255, alpha: 1) 
      let image = UIImage(named: "eh") 
      cell.backgroundView = UIImageView(image: image?.resizableImageWithCapInsets(UIEdgeInsets(top: 20, left: 20, bottom: 50, right: 50))) 
     } else { 
      cell.commentText.textColor = UIColor.whiteColor() 
      let image = UIImage(named: "whatsthatnow") 
      cell.backgroundView = UIImageView(image: image?.resizableImageWithCapInsets(UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 100))) 
     } 
    } 
    cell.layoutMargins = UIEdgeInsetsZero 
    //cell.backgroundView = UIImageView(image: UIImage(named: "goodbg")) 
    return cell 
} 

但是,這對於特定的視圖控制器來說工作得很好。請注意這段代碼片段。

func tableViewOld(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> UITableViewCell! { 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell 
    let image = UIImage(named: "eh") 
    cell.backgroundView = UIImageView(image: image?.resizableImageWithCapInsets(UIEdgeInsets(top: 20, left: 20, bottom: 50, right: 50))) 

此相同的代碼由於某種原因在另一個單元上不起作用。我似乎無法找到它的原因,所以我希望你們中的一個人能夠發現我不能做的事情。

回答

0

檢查UITableViewCell的.h文件。我找到了這個。

對於UITableViewStylePlain中的單元格,默認值爲零,對於,默認值爲零。 backgroundView將作爲所有其他視圖背後的子視圖添加。

@property (nonatomic, retain) UIView *backgroundView; 

通常情況下,我們不做任何提示:

  1. 添加ImageView到細胞。
  2. 設置ImageView的幀等於cell.contentView.bounds
  3. 控制它的行爲由你自己。
  4. 做你想做的事!