2016-07-16 50 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