2015-10-05 90 views
1

我有一個UITableView,它包含帶滑塊和兩個標籤的單元格,每當滑塊不在視圖中時,它就會在當前內容的頂部再次繪製。UITableViewCell在滾動上重繪

這是一個gif解釋我的意思。

http://i.imgur.com/4dYtyJy.gifv

這裏是相關的代碼。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let CellIdentifier: String = "\(indexPath.row) - \(indexPath.section)" 
    var cell: UITableViewCell! = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier) 

    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: CellIdentifier) 
    } 

    let label = UILabel(frame: CGRect(x: 16.0, y: 16.0, width: 300, height: 30.0)) 
    let percentageLabel = UILabel(frame: CGRect(x: 16.0, y: 0, width: 200.0, height: 30.0)) 
    let slider = CustomUISlider(frame: CGRect(x: 16.0, y: 55.0, width: 300.0, height: 20.0)) 

    slider.maximumTrackTintColor = Global().turqTint 
    slider.minimumTrackTintColor = Global().blueTint 
    slider.minimumValue = 0.0 
    slider.maximumValue = 1.0 
    slider.value = 0.0 
    slider.tag = indexPath.row 
    slider.setThumbImage(UIImage(named: "sliderThumbImage"), forState: .Normal) 
    slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged) 

    label.text = Array(selectedTypes)[indexPath.row].1 

    percentageLabel.text = "\(slider.value)" 
    percentageLabel.tag = indexPath.row 

    cell.tintColor = Global().tintColor 
    cell.addSubview(label) 
    cell.addSubview(slider) 
    cell.addSubview(percentageLabel) 

    return cell 
} 
+3

iOS將在出現時調用cellForRowAtIndexPath。由於您每次都爲其添加子視圖,因此它將在滾動時添加新的滑動視圖。嘗試將它們移動到if cell == nil block – Horst

+0

謝謝@Horst - 你想提交這個答案作爲答案,所以我可以將它標記爲正確的嗎? –

回答

0

我有類似的問題。正如霍斯特在評論中說,把該片斷塊內將這樣的伎倆:

if (cell == nil) 
{ 
    // Code that draws frames 
} 
0

如果硬要去用你的方式,你想有更多的東西一樣 (注意"\(indexPath.row) - \(indexPath.section)")這將爲每個單元創建不同的CellID。你想有一些共同的CellID,以便能夠從TableView中的重用邏輯受益:

//Global constants for the View tags 
let textLabelTag = 1 
let percentageLabelTag = 2 
let sliderTag = 3 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let CellIdentifier: String = "SomeUniqueID" 
    var cell: UITableViewCell! = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier) 

    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: CellIdentifier) 
     self.createUIforCell(cell); 
    } 

    self.configureCell(cell, indexPath: indexPath) 

    return cell 
} 
  1. 爲細胞創建UI組件,並將其添加爲子視圖。這種方法將被調用一次,如果電池不存在:

    func createUIforCell(cell: UITableViewCell) { 
    
        let textLabel = UILabel(frame: CGRect(x: 16.0, y: 16.0, width: 300, height: 30.0)) 
        textLabel.tag = textLabelTag 
        cell.addSubview(textLabel) 
    
        let percentageLabel = UILabel(frame: CGRect(x: 16.0, y: 0, width: 200.0, height: 30.0)) 
        percentageLabel.tag = percentageLabelTag 
        cell.addSubview(percentageLabel) 
    
        let slider = CustomUISlider(frame: CGRect(x: 16.0, y: 55.0, width: 300.0, height: 20.0)) 
        slider.tag = sliderTag 
        slider.maximumTrackTintColor = Global().turqTint 
        slider.minimumTrackTintColor = Global().blueTint 
        slider.minimumValue = 0.0 
        slider.maximumValue = 1.0 
        slider.setThumbImage(UIImage(named: "sliderThumbImage"), forState: .Normal) 
        slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged) 
        cell.addSubview(slider) 
    } 
    
  2. 更新UI正確的數據:

    func configureCell(cell: UITableViewCell, indexPath: NSIndexPath) { 
    
        let textLabel: UILabel = cell.viewWithTag(textLabelTag) as! UILabel 
        textLabel.text = Array(selectedTypes)[indexPath.row].1 
    
        let slider: CustomUISlider = cell.viewWithTag(sliderTag) as! CustomUISlider 
        slider.value = 0.0 
    
        let percentageLabel: UILabel = cell.viewWithTag(percentageLabelTag) as! UILabel 
        percentageLabel.text = "\(slider.value)" 
    
    } 
    

注意:這將是更清潔如果你有自己創建UI的UITableViewCell子類(在代碼中或在.xib中)

0

你應該子類UITableViewCell並覆蓋prepeareForReuse方法。有了它,您可以將您的手機設置爲默認模式。