2016-05-05 95 views
1

我在我的表格視圖中有幾行,每個行都是我的自定義單元類的一個實例,它包含單個UTextField。我爲每個標籤分配了一個標籤,但我需要知道如何檢索每個文本字段的文本值並將值分配給適當的字符串。從表格視圖中的文本字段獲取文本

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("paymentCell", forIndexPath: indexPath) as! PaymentInfoCell 

     cell.selectionStyle = .None 
     cell.textField.delegate = self 

     if indexPath.section == 0 { 
      cell.textField.keyboardType = .NumberPad 

      switch indexPath.row { 
      case 0: 
       cell.textField.placeholder = "Card Number" 
       cell.textField.tag = 0 
      case 1: 
       cell.textField.placeholder = "Security Code" 
       cell.textField.tag = 1 
      case 2: 
       cell.textField.placeholder = "Expiration Month" 
       cell.textField.tag = 2 
      case 3: 
       cell.textField.placeholder = "Expiration Year" 
       cell.textField.tag = 3 
      default: 
       break 
      } 
     } else { 
      switch indexPath.row { 
      case 0: 
       cell.textField.placeholder = "First Name" 
       cell.textField.keyboardType = .Default 
       cell.textField.tag = 4 

       if let firstName = sharedUser.userJSON!["firstName"] { 
        cell.textField.text = firstName as? String 
       } 
      case 1: 
       cell.textField.placeholder = "Last Name" 
       cell.textField.keyboardType = .Default 
       cell.textField.tag = 5 

       if let lastName = sharedUser.userJSON!["lastName"] { 
        cell.textField.text = lastName as? String 
       } 
      case 2: 
       cell.textField.placeholder = "Phone Number" 
       cell.textField.keyboardType = .PhonePad 
       cell.textField.tag = 6 
      case 3: 
       cell.textField.placeholder = "Address" 
       cell.textField.keyboardType = .EmailAddress 
       cell.textField.tag = 7 
      case 4: 
       cell.textField.placeholder = "City" 
       cell.textField.keyboardType = .Default 
       cell.textField.tag = 8 
      case 5: 
       cell.textField.placeholder = "State" 
       cell.textField.keyboardType = .Default 
       cell.textField.tag = 9 
      case 6: 
       cell.textField.placeholder = "Zip Code" 
       cell.textField.keyboardType = .NumberPad 
       cell.textField.tag = 10 
      default: 
       break 
      } 
     } 

     return cell 
    } 

我可以做到這一點,但它似乎並不是最好的選擇。

func textFieldShouldReturn(textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 

     switch textField.tag { 
     case 0: 
      self.cardNumber = textField.text! 
     case 1: 
      self.securityCode = textField.text! 
     case 2: 
      self.expMonth = textField.text! 
     case 3: 
      self.expYear = textField.text! 
     case 4: 
      self.firstName = textField.text! 
     case 5: 
      self.lastName = textField.text! 
     case 6: 
      self.phoneNumber = textField.text! 
     case 7: 
      self.address = textField.text! 
     case 8: 
      self.city = textField.text! 
     case 9: 
      self.state = textField.text! 
     case 10: 
      self.zipCode = textField.text! 
     default: 
      break 
     } 

     return true 
    } 
+0

我不確定你在問什麼。 – Alexander

回答

0

您可以簡單地創建佔位符這樣

let placeholders = ["Card Number", "Security Name", ...] 

一個數組,那麼你可以使用此代碼是不是多餘

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("paymentCell", forIndexPath: indexPath) as! PaymentInfoCell 

    cell.selectionStyle = .None 
    cell.textField.delegate = self 
    let index = indexPath.row + indexPath.section*6 //calculates the index of the text in the array 
    cell.textField.placeholder = placeholders[index] 
    cell.textField.tag = index 

    if indexPath.section == 0 { 
     cell.textField.keyboardType = .NumberPad 
    } else { 
     if indexPath.row == 0 { 
      if let firstName = sharedUser.userJSON!["firstName"] { 
       cell.textField.text = firstName as? String 
      } 
     } else if indexPath.row == 1 { 
      if let lastName = sharedUser.userJSON!["lastName"] { 
       cell.textField.text = lastName as? String 
      } 
     } 
    } 
    return cell 
} 

你可以做同樣的你變量,把它們放在一個數組中,只是說

variable[textField.tag] = textField.text! 
相關問題