从表格视图单元格内的文本字段中获取文本

Posted

技术标签:

【中文标题】从表格视图单元格内的文本字段中获取文本【英文标题】:Get Text from Text Field Inside Table View Cell 【发布时间】:2016-05-05 19:08:20 【问题描述】:

我的表格视图中有几行,每行都是我的自定义单元格类的一个实例,其中包含一个 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
    

【问题讨论】:

我不确定你在问什么。 【参考方案1】:

您可以像这样简单地创建一个占位符数组

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!

【讨论】:

以上是关于从表格视图单元格内的文本字段中获取文本的主要内容,如果未能解决你的问题,请参考以下文章

如何从表格视图中的单元格点击图像视图

如何从表格视图的自定义单元格内的文本字段中获取值(标签)以发布到 URL [重复]

在动态表格视图中获取文本字段值

从 Swift (3.1) 表格视图中动态创建的单元格获取有序的文本字段数据

一次从所有集合视图单元格内的文本视图中获取文本

如何以编程方式在单元格内创建文本字段或标签?