【问题标题】:tableview data repeat when scrolling滚动时tableview数据重复
【发布时间】:2016-08-28 09:42:56
【问题描述】:

我使用自定义单元格显示placeholder,但滚动表格会重复placeholder

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! AddTableViewCell

    cell.textInfo.delegate = self
    if textPlaceHolders![indexPath.row].containsString("Category") == true {
        cell.selected = true
        cell.textInfo.text = textPlaceHolders![indexPath.row]
        cell.accessoryType = .DisclosureIndicator
    } else {
        cell.textInfo.placeholder = textPlaceHolders![indexPath.row]
    }
    return cell
}

我尝试了一些这样的解决方案,问题解决了,但是当用户端编辑时,文本消失了

class AddTableViewCell: UITableViewCell {

  @IBOutlet weak var textInfo: UITextField!

  override func prepareForReuse() {
     textInfo.text= ""
  }

}

【问题讨论】:

    标签: ios swift uitableview uitextfield


    【解决方案1】:

    在您的情况下,您在一种情况下为单元格的textInfo 插座分配text 属性,在另一种情况下为placeholder 分配。由于UITableView 的重用策略,您的textInfo 包含占位符/文本,即使您没有为具体indexPath 指定它。因此,如果您不想要它们,则需要为每个 indexPath 清理它。像这样:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! AddTableViewCell
        cell.textInfo.delegate = self
        if textPlaceHolders![indexPath.row].containsString("Category") == true {
            cell.selected = true
            cell.textInfo.text = textPlaceHolders![indexPath.row]
            cell.textInfo.placeholder = nil
            cell.accessoryType = .DisclosureIndicator
        } else {
            cell.textInfo.placeholder = textPlaceHolders![indexPath.row]
            cell.textInfo.text = nil
        }
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-27
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多