2015-06-21 3 views
0

У меня есть настройка UITableView с пользовательским UITableViewCell, в котором есть метка, а также текстовое поле. Всякий раз, когда я меняю текст текстового поля, меняется и текст в текстовом поле случайной другой ячейки.UITableView Изменить текст в одной ячейке, текст в других изменениях также

public class ChangePriceTableViewCell: UITableViewCell { 

    @IBOutlet weak var priceField: UITextField! 
    @IBOutlet weak var productName: UILabel! 
    @IBAction func changePrice(sender: UITextField) { 
     println(sender.text) 
    } 

    public override func layoutSubviews(){ 

     var toolbar = UIToolbar.new() 
     toolbar.frame.size.height = 35 

     var doneButton:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "hideKeyboard") 

     var space:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) 

     var items = [AnyObject]() 
     items.append(space) 
     items.append(doneButton) 

     toolbar.items = items 

     priceField.inputAccessoryView = toolbar 
    } 

    public func configure(product: String) { 
     self.productName.text = product 


    } 

    func hideKeyboard() { 
     println("Test") 
     priceField.resignFirstResponder() 
    } 


} 

И DataSource:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 12//self.products.keys.count 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     //println(self.products[self.products.keys[section]]) 
     return 2//self.products[self.products.keys[section]]!.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("change") as! ChangePriceTableViewCell 

     //var prods = self.products[self.products.keys[indexPath.section]] 
     //var productName = prods?[indexPath.row] as! String 
     //cell.configure(productName) 

      cell.configure("Test") 
     return cell 
    } 

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return "Test"//self.products.keys[section].uppercaseString 
    } 

Клетка, которая изменяет всегда закадровый. Иногда значения даже прыгают между двумя ячейками!

ответ

2

Вы являетесь ячейками повторно. Внесите этот метод в ChangePriceTableViewCell:

func prepareForReuse() // if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:. If you override, you MUST call super. 

И установите правильное значение по умолчанию для ячейки.