2016-10-29 75 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