单元格上的自定义表格视图滚动问题

Custom tableview scrolling issue on cells

我有一个以编程方式创建的表视图

var tableView: UITableView  =   UITableView()
var items = ["1","2","3","4","5","6"," 1","L 2","La 3","La 4","La 5","La 6","L 7","La 8","La 9","La 10","La 11","Lab 12","Lab 13","Lab 14","Lab 15","Lab 16","Lab 17","Lab 18","Lab 19","Lab 20","La 1","L 2","La 3"]

   override func viewDidLoad() {
    super.viewDidLoad()
    tableView.frame         = CGRectMake(0, 50, self.view.frame.width,100);  //If we give self.view.frame.height It worked because no need to scroll
    tableView.delegate      = self
    tableView.dataSource  =  self
    tableView.estimatedRowHeight = 30
   tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.view.addSubview(tableView)
    }

这是有问题的代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
        if indexPath.row == 2 || indexPath.row == 3 {
            var redBtn = UIButton()
            redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
            redBtn.backgroundColor = UIColor.redColor()
            redBtn.setTitle("das", forState: UIControlState.Normal)         
            cell.contentView.addSubview(redBtn)
        }else {
        cell.textLabel?.textAlignment = NSTextAlignment.Center
        cell.textLabel?.text = items[indexPath.row]
       }
       return cell
   }
}

在第 2 行和第 3 行,当滚动它显示在所有 cells.If 中的 tableview 时,我只需要显示 button.But 将 tableview 高度增加到视图高度然后没问题,因为那里没有滚动发生。

我从中读到这个问题 UITableView scrolling and redraw issue 我发现这是因为重用单元问题,而 scrolling.But 该解决方案对我不起作用。

合适的解决方案是 welcome.I 提供了完整的代码,因此您可以将其复制并粘贴到项目中,然后解决问题。

提前致谢

当您重复使用单元格时,旧值仍然存在,因此每次您使用 dequeueReusableCellWithIdentifier 时,您都需要重置为默认值或仍然缓存在其中的最后一个值。在您的特定情况下,您需要从单元格中删除您创建的按钮或将其设置为隐藏在您的 else 语句中。

From apple documentation:

The table view’s data source implementation of tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.

实现这一点的最佳方法是创建一个自定义单元格,其中包含您需要的所有组件(按钮、标签等),同时将新的自定义单元格出队,将 属性 隐藏设置​​为 true或根据需要为 false。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCustomCell
        if indexPath.row == 2 || indexPath.row == 3 {
            redBtn.hidden = false
            cell.contentView.addSubview(redBtn)
        }else {
            redBtn.hidden = true
            cell.textLabel?.textAlignment = NSTextAlignment.Center
            cell.textLabel?.text = items[indexPath.row]
      }
      return cell

在这种情况下,以编程方式即时创建按钮不是一个好主意,因为您必须循环单元格中的所有子视图以查找每个可重用单元格是否已经存在,从而决定您是否需要创建或删除,如下所示:

for button:AnyObject in subviews{
    if(button .isKindOfClass(UIButton)){
       if(button.accessibilityIdentifier == "MyButton"){
            println("Button Already Exists")
        }else{
            println("Create new button")
        }
    }
}

希望对你有帮助