2016-02-29 16 views
0

我有這樣的TableView異常行爲添加多個小區的TableView中

enter image description here

當視圖顯示它工作正常,當我添加一行它工作正常過,當我添加第二個,第三,ECC ...排它開始有這個問題:

enter image description here

(而不是隻具有段下去的時候,它會顯示兩者)

Segmet是建立與固定點圖像和中間兩個部分:一個up和一個d自己的,並從該行根據,我展示一個頂段,或者只有底部或兩個:

cell.centralDotIV.image = UIImage(named: "Dot") 
    if array.count > 1 { 
     if indexPath.row == 0 { 
      cell.lowerLineIV.image = UIImage(named: "Line") 
     } else if indexPath.row == array.count - 1 { 
      cell.upperLineIV.image = UIImage(named: "Line") 
     } else { 
      cell.upperLineIV.image = UIImage(named: "Line") 
      cell.lowerLineIV.image = UIImage(named: "Line") 
     } 
    } 

這裏的那裏每一次按鍵增加該小區獲得拍了拍代碼:

@IBAction func addDateTapped(sender: UIBarButtonItem) { 
    array.append(NSDate()) 

    var i = 0 
    var indexArray = [NSIndexPath]() 
    for _ in array { 
     let index = NSIndexPath(forRow: i, inSection: 0) 
     indexArray.append(index) 
     i++ 
    } 

    tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) 
    if array.count != 1 { tableView.reloadRowsAtIndexPaths(indexArray, withRowAnimation: .Fade) } 
} 

我試過甚至每次點擊按鈕時重新加載表格都是一樣的。怎麼了?

回答

0

您的單元出隊時沒有正確更新您的單元格。你必須取消你不需要的圖像。

cell.centralDotIV.image = UIImage(named: "Dot") 
if array.count > 1 { 
    if indexPath.row == 0 { 
     cell.lowerLineIV.image = UIImage(named: "Line") 
     cell.upperLineIV.image = nil 
    } else if indexPath.row == array.count - 1 { 
     cell.lowerLineIV.image = nil 
     cell.upperLineIV.image = UIImage(named: "Line") 
    } else { 
     cell.upperLineIV.image = UIImage(named: "Line") 
     cell.lowerLineIV.image = UIImage(named: "Line") 
    } 
} 

而且你不應該經常玩image屬性。相反,image設置爲UIImage(named: "Line")一勞永逸,並在你的控制器:

cell.centralDotIV.image = UIImage(named: "Dot") 
if array.count > 1 { 
    if indexPath.row == 0 { 
     cell.lowerLineIV.hidden = false 
     cell.upperLineIV.hidden = true 
    } else if indexPath.row == array.count - 1 { 
     cell.lowerLineIV.hidden = true 
     cell.upperLineIV.hidden = false 
    } else { 
     cell.upperLineIV.hidden = false 
     cell.lowerLineIV.hidden = false 
    } 
} 

您可以因式分解:

cell.lowerLineIV.image = UIImage(named: "Linea") 
    cell.upperLineIV.image = UIImage(named: "Linea") 

    if DataManager.sharedInstance.actualAction.iteration_action!.count > 1 { 
     cell.lowerLineIV.hidden = indexPath.row == array.count - 1 
     cell.upperLineIV.hidden = indexPath.row == 0 
    } else { 
     cell.lowerLineIV.hidden = true 
     cell.upperLineIV.hidden = true 
    } 
+0

你說得對,謝謝! –

+0

隨着你的「因素分析」你贏得了一切! –

0

@ Matte.Car彷彿array.count = 1你重裝!每次插入新記錄時,表格視圖的所有行。那麼,是否有可能使用tableView.reloadData()方法?其次,你可以顯示你的代碼CellForRowAtIndexPath()。

0

由於性能原因,表格視圖單元格通常會重新使用。所以你需要完全配置它們或者實現合適的「prepareForReuse」模式。