Sam
Sam

Reputation: 235

tableview data repeat when scrolling

I use a custom cell to show the placeholder but scrolling the table repeats the 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
}

I tried some solution like this the problem resolved but when user end edit the text disappear

class AddTableViewCell: UITableViewCell {

  @IBOutlet weak var textInfo: UITextField!

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

}

Upvotes: 1

Views: 530

Answers (1)

rkyr
rkyr

Reputation: 3251

In your case you assign text property for cell's textInfo outlet in one case and placeholder in another. Because of UITableView's reuse policy your textInfo contains placeholder/text even if you haven't specified it for concrete indexPath. So you need to clean it up for every indexPath if you don't want them. Like this:

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
}

Upvotes: 1

Related Questions