2016-10-29 111 views
1

我有一個數組類型的字符串數組,我在for循環內輸出tableView。我知道它在cellForRowAt indexPath內部是一個不好的練習循環:function但我沒有solution.My問題是我每次移動時我tableview模擬器上,我插入更多的子視圖上現有ones.it覆蓋舊的,防止我使用當我刪除細胞子視圖時,Swift 3 UITableView細胞分離器消失

for view in cell.subviews { 
     view.removeFromSuperview() 
} 

,但它會刪除我的細胞分隔符與我的大電池標籤。我怎樣才能刪除單元格數據不是我的分隔符。

func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 

    var cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 
    //print(cell.subviews) 
    for view in cell.subviews { 
     view.removeFromSuperview() 
    } 


    for i in 0..<globalHeaderStructArray.count{ 
     var newLabel = UILabel(frame: CGRect(x:xCoordination, y:10, width:Double(globalHeaderStructArray[i].width)!, height:30.0)) 
     newLabel.font = UIFont(name: "Avenir", size: 17.0) 
     newLabel.textColor = UIColor.darkGray 
     newLabel.text = "\(globalDataArray[indexPath.row][i])" 

     var scrollview = UIScrollView(frame: cell.bounds) 
     scrollview.contentSize = CGSize(width:cell.bounds.width * 5, height:cell.bounds.height) // will be 5 times as wide as the cell 
     scrollview.isPagingEnabled = true 

     cell.contentView.addSubview(scrollview) 
     cell.addSubview(newLabel) 
     xCoordination += Double(globalHeaderStructArray[i].width)! 
    } 
    xCoordination = 0.0 

    return cell 

} 

回答

0

您可以設置tag到標籤和scrollView對象和檢查內部循環這樣的。

for view in cell.subviews { 
    if view.tag == 101 || view tag == 102 { 
     view.removeFromSuperview() 
    } 
} 


for i in 0..<globalHeaderStructArray.count{ 
    var newLabel = UILabel(frame: CGRect(x:xCoordination, y:10, width:Double(globalHeaderStructArray[i].width)!, height:30.0)) 
    newLabel.font = UIFont(name: "Avenir", size: 17.0) 
    newLabel.textColor = UIColor.darkGray 
    newLabel.text = "\(globalDataArray[indexPath.row][i])" 
    newLabel.tag = 101 

    var scrollview = UIScrollView(frame: cell.bounds) 
    scrollview.contentSize = CGSize(width:cell.bounds.width * 5, height:cell.bounds.height) // will be 5 times as wide as the cell 
    scrollview.isPagingEnabled = true 
    scrollview.tag = 102 

    cell.contentView.addSubview(scrollview) 
    cell.addSubview(newLabel) 
    xCoordination += Double(globalHeaderStructArray[i].width)! 
} 

提示:這是連擊,如果你使用Interface Builder來設計,而不是在運行時將UI元素你的。

+0

如果我們不鼓勵使用模糊標籤,除非100%絕對必要(這對於傳統架構問題來說極其罕見並且通常是錯誤的)。如果您需要參考,請使用指針..... – TheCodingArt