【问题标题】:How to get the textvalue of each dynamically created row in UITableView in swift如何快速获取UITableView中每个动态创建的行的textvalue
【发布时间】:2015-08-31 13:14:31
【问题描述】:

如下图所示,我在 tableviewController 中创建了一个带有标签和文本字段的原型单元格。

每一行都是在运行时创建的,如下所示。当用户单击“保存”按钮时,课程详细信息、注册 ID、用户名和密码等所有详细信息都应保存在相应的变量中。但它不起作用。它将最后一个文本字段的值存储在所有变量中。

如何获取表格每一行的文本值。

// 行创建代码

cell variable is global
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        switch(indexPath.row)
        {
            case 0 :
                 cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
         cell.configure("Course detail", txtValue: "Enter course.")
                 break;

            case 1 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Registration ID", txtValue: "Enter registration id.")
                cell.txtIpValue.tag = 1
                break;

            case 2 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Username ", txtValue: "Username")
                cell.txtIpValue.tag = 2
                break;

            case 3 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Password ", txtValue: "Password")
                cell.txtIpValue.tag = 2
                break;

            default :
                break;

        }


        return cell
    }

// 保存按钮代码

func saveButtonClicked() {

        if(cell.txtIpValue.tag == 1)
        {
            strCourse = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 2)
        {
            strID = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 3)
        {
            strUsername = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 1)
        {
            strPassword = cell.txtIpValue.text
        }      

}

// 正则单元

class RegCell: UITableViewCell {

    @IBOutlet var txtIpValue: UITextField
    @IBOutlet var lblstr: UILabel

    func configure(lblValue : String,txtValue :String)) {

        lblstr.text = lblValue
        txtIpValue.text = txtValue
    } 

}

所有代码都在 swift 中。可以的话请举个例子。

【问题讨论】:

  • 在您的应用程序中 4 个单元格是固定的吗?
  • saveButtonClicked 中的单元格变量来自哪里?
  • @Chetan Prajapati - 是的
  • @deadbeef - 抱歉忘记提供单元格变量是全局变量的信息。
  • 那么为什么你不采用静态单元格而不是动态单元格。 bcoz 4 单元的内存可靠。没什么大不了的。上面代码中的另一件事也有很多错误

标签: ios iphone swift uitableview uitextfield


【解决方案1】:

遍历 tableView 中的所有单元格。 假设你只有一个部分,试试这个:

更新:使用标签文本作为值的键 - 您可能需要考虑为其查找常量值

func save() {
    var fields = NSMutableDictionary();
    for rowIndex in 0...self.tableView.numberOfRowsInSection(0) {
        let indexPath : NSIndexPath = NSIndexPath(forItem: rowIndex, inSection: 0);
        let cell : RegCell = self.tableView.cellForRowAtIndexPath(indexPath);
        fields.setObject(cell.txtIpValue.text, forKey: cell.lblstr.text)
    }
    // ... here fields will contain all values, using the lblstr.text as key
    let username = fields["Username "];
}

【讨论】:

  • 我建议您将标识符属性放入 RegCell,例如“user_name”或“password”,并将它们用作我上面建议的 mutableDictionary 中的键。这会将其与实际显示的标签文本分开。
  • 非常好的答案,你拯救了我的一天@MarkHim
猜你喜欢
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
相关资源
最近更新 更多