簡體   English   中英

在uiableview中從uitextfield獲取價值

[英]getting value from uitextfield in uitableview

我里面有很多uitextfields的uitableviewcells。 而且我正在使用textField的委托來捕獲用戶的編輯。 只要我不使用部分,效果就很好。 因為我使用textField的標簽來保存單元格的indexPath.row。 問題是我現在必須使用節,並且必須以某種方式使用這些節來保存indexPath.row和.section。

這是一些代碼。

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)
}

有沒有更好的方法來捕獲文本字段的編輯? 或者如何將這兩種信息都保存在文本字段的標記中?

問候阿達卡斯。

讓我們采取不同的方法。
除了使用標記,您還可以轉換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)
}

我不記得我在哪里看到這應該給予應有的榮譽,但這就是我在應用程序中使用的東西。 您將基於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)

我已經解決了如下問題。

我為數據源創建了唯一的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了。

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

此輔助函數僅希望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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM