我有一个数组类型的字符串数组,我将它打印到for tableView
for for循环。我知道它在cellForRowAt indexPath
内的一个不好的练习循环:function但我没有任何解决方案。我的问题是每次我在模拟器上移动tableview
时,我会在现有的子视图上插入更多的子视图。它会覆盖较旧的子视图并阻止我使用
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 :(得分: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)!
}
提示:如果您使用界面构建器来设计单元格而不是在运行时添加UI
元素,则会很糟糕。