2016-02-29 31 views
0

我有很多uitablextfields裏面有uitableviews。我正在使用textField的委託捕捉用戶的編輯。只要我不使用部分,這工作正常。因爲我使用textField的標籤來保存單元格的indexPath.row。問題是我必須現在使用部分,並且我必須以某種方式保存indexPath.row和.section。在uitableview中從uitextfield獲得價值

這是一些代碼。

func textFieldDidEndEditing(textField: UITextField) { 
    let indexPath = NSIndexPath(forRow: textField.tag, inSection: 0) 
    let cell : UITableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell? 
    if data[(cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text!] != textField.text { 
    newData[fields[indexPath.row].ID] = textField.text 
    } 
    print((cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text) 
    print(textField.text) 
} 

有沒有更好的方法來捕捉文本字段的編輯?或者我怎麼能將這兩個信息保存在文本字段的標記中?

問候Adarkas。

回答

0

我已經解決了我的問題,如下所示。

我已經爲我的數據源創建了一個唯一的ID,我用它來確定單元格已被更新。

var row = 0 
var section = 0 

for var k = 0; k < fieldsWSections.count; k++ { 
    for var kk = 0; kk < fieldsWSections[k].count; kk++ { 
    if fieldsWSections[k][kk].ID == String(textField.tag) { 
     section = k 
     row = kk 
    } 
    } 
} 

if data[fieldsWSections[section][row].name] != textField.text { 
    newData[fieldsWSections[section][row].ID] = textField.text 
} 

所以我不需要知道indexPath了。

0

讓我們採取不同的方法。
而不是使用標記,您可以轉換您的textfield位置並獲取其單元超級視圖的索引路徑。

func textFieldDidEndEditing(textField: UITextField) { 
    let origin: CGPoint = textField.frame.origin 
    let point: CGPoint = textField.convertPoint(origin, toView: self.tableView) 

    let indexPath = self.tableView.indexPathForRowAtPoint(point) 
    let cell : UITableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell? 
    if data[(cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text!] != textField.text { 
    newData[fields[indexPath.row].ID] = textField.text 
    } 
    print((cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text) 
    print(textField.text) 
} 
+0

恩,這是一個不錯的主意,謝謝。我會測試一下。 – Adarkas2302

+0

我發現了一個解決方案,我自己,但我仍然會嘗試你的建議,因爲我覺得它很有趣。謝謝你的幫助。 – Adarkas2302

+2

@ Adarkas2302:如果你發現了這個問題的解決方案,你應該發佈一個答案並自己接受。這將有助於未來的讀者,當他們有與你的相同的問題。 –

0

我不記得在哪裏,所以我看到這給予適當的功勞,但這裏是我在我的應用程序中使用的。你得到基於textField的超級視圖的indexPath。

let textField = sender as! UITextField 
let view = textField.superview!.superview! 
let currentCell = view.superview as! UITableViewCell // Or substitute your custom cell class name 
let indexPath = tableView.indexPathForCell(currentCell) 
+0

好吧,我得到一個錯誤'let currentCell = view.superview as! UITableViewCell'。我試圖做到這一點,我認爲我得到了錯誤,因爲我的自定義單元格。但我有大約9個隨機分佈在tableview中的自定義單元格。 – Adarkas2302

0

對可重用代碼使用標籤是一個壞主意,但其他答案依賴於層次結構的精確結構。

這個幫助函數只是期望UITextField是UITableViewCell的一個子類,而UITableViewCell是它們的UITableView的子類,當UITextFieldDelegate被調用時它應該總是爲true。如果其中任何一個不成立,則返回nil。

func indexPath(for view: UIView) -> IndexPath? { 
     // find the cell 
     var sv = view.superview 
     while !(sv is UITableViewCell) { 
     guard sv != nil else { return nil } 
     sv = sv?.superview 
     } 
     let cell = sv as! UITableViewCell 

     // find the owning tableView 
     while !(sv is UITableView) { 
     guard sv != nil else { return nil } 
     sv = sv?.superview 
     } 
     let tableView = sv as! UITableView 

     // locate and return the indexPath for the cell in this table 
     return tableView.indexPath(for: cell) 
}