2016-07-16 62 views
2

我在UITableViewCell中创建一个窗体,此窗体在原型单元格中具有标签和文本字段。在这里看到它的图片 TableView Story board Image。 我使用标识符动态创建的形式,我的代码是UITableViewCell滚动后的数据更改

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("protocell", forIndexPath: indexPath) as! UITableViewCell 

    var lable = cell.viewWithTag(1) as! UILabel 
    lable.text = details[indexPath.row] 
    lable.textColor = UIColor.brownColor().colorWithAlphaComponent(0.7) 

    var textfield = cell.viewWithTag(2) as! UITextField 
    textfield.placeholder = "Enter \(details[indexPath.row])" 
    self.arrayTextField.append(textfield) 
    if details[indexPath.row] == "Name" { 
     textfield.placeholder = "Enter First \(details[indexPath.row]) Middle \(details[indexPath.row]) Last \(details[indexPath.row]) " 
    } 
    textfield.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.2) 

    return cell 
} 

现在的问题是我有18个字段,并且当我在字段中输入值和滚动视图以填充剩余的字段,则值更改字段中的更改。 请帮忙。

+0

您需要跟踪数据源中哪些文本字段包含文本。所以当在tableView:(tableView:cellForRowAtIndexPath:)中有一个时,你放了它,否则,你放置你的placeHolder,因为单元格被重用。 – Larme

+0

**从不**使用包含表格视图单元的UI元素的数组('arrayTextField')。这可能是你的问题的原因。考虑改进(数据源)模型的设计,更改模型中的数据并重新加载表视图。 – vadian

+0

跟踪我使用'arrayTextField'的数据源@Larme如何跟踪数据源以查看哪个文本字段有文本。我搜查了但没有发现任何东西。 – knownUnknown

回答

2

创建UITableViewCell子类并覆盖prepeareForReuse函数 - 将单元格变为默认模式。

斯威夫特:

override func prepareForReuse() { 
    super.prepareForReuse() 

    //set cell to initial state here, reset or set values, etc. 
} 

根据您的评论 - 如何继承UITableViewCell

import UIKit 

class MyCustomCell: UITableViewCell { 
    // things that you can do here: 
    // initialise your properties here. (label, textfield. etc) 
    // layout subviews. 
    // override superclass APIs. 
} 
+0

我是iPhone应用程序开发的初学者,所以@Evegeny Karkan可以告诉我如何创建子类。 – knownUnknown

+0

@AdnanMomin在我的答案上看到更新。 –

0
  1. 不要存放在数组中的文本框,而不是数据源“细节”阵列应该这样做。
  2. 一旦你在textfield中输入了任何东西,你就会更新你indexPath的“details”数组。
  3. 子类的UITableViewCell,并作出CustomTableViewCell,并有IBOutlets为您文本框的标签,什么不能让您的生活更轻松
  4. 对你CustomTablewViewCellupdateWithDetails(details)使代码很好地封装
的方法
+0

my ** details **数组,包含字段的标签,所以我无法更新它。 – knownUnknown

+0

,然后从数组中删除这些。您的详细信息数组应包含一些对象,如EmployeesDetails * employeeDetails,然后数组应该是employeDetailsArray。然后对于每个EmployeeDetails可以将“信息”作为字符串而不是文本字段。你的dataSource不应该包含任何UI元素使它们分开。 – hariszaman

+0

我的details数组就像'details = [“name”,“address”,contactnumber“,]'等等。 – knownUnknown