1
我做了一個簡單的項目,其中我將UITableViewCell
轉換爲測試drawrect:
方法。在cellForRowAtIndexPath
我設置了每個其他單元的背景backgroundColor
。它適用於模擬器,但不適用於設備。 我知道有更簡單的方法來獲取交替單元格背景,但這只是一個簡單的例子,表明drawRect在這種情況下似乎不適用於我的設備。自定義的UITableViewCell不能在設備上運行[drawRect :) [Swift]
這裏是我的代碼:
// The subclassed custom cell
class CustomCell: UITableViewCell {
var fColor: UIColor?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
fColor = UIColor.clearColor()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fColor = UIColor.clearColor()
}
/*
func setFcolor(c:UIColor){
fColor = c
setNeedsDisplay()
}
*/
override func drawRect(rect: CGRect) {
var context: CGContextRef = UIGraphicsGetCurrentContext()
var color: UIColor = fColor!
color.set()
CGContextFillRect(context, rect)
}
}
// My cell creation
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomCell
cell.fColor = indexPath.row % 2 == 0 ? UIColor.whiteColor() : UIColor.blackColor()
cell.textLabel.text="row#\(indexPath.row)"
return cell
}
這裏是故事板的設置:
這裏是在模擬器上的預期結果:
但我的設備(iOS7.1)上的每一個細胞背景是白色的
謝謝,但這不是我的答案尋找。就像我說的有更簡單的方法來實現交替的單元格顏色。這只是使用'drawRect:'的一個例子,它不在設備上顯示,而是在模擬器上顯示。 – 2014-11-16 21:06:01