Ios 如何在swift的UITableView中获取每个动态创建行的textvalue
如下图所示,我在tableviewController中创建了一个带有标签和文本字段的原型单元格 每一行都是在运行时创建的,如下所示。当用户单击“保存”按钮时,所有详细信息(如课程详细信息、注册id、用户名和密码)都应保存在相应的变量中。但它不起作用。它在所有变量中存储最后一个文本字段的值 如何获取表中每一行的文本值 //行创建代码Ios 如何在swift的UITableView中获取每个动态创建行的textvalue,ios,iphone,swift,uitableview,uitextfield,Ios,Iphone,Swift,Uitableview,Uitextfield,如下图所示,我在tableviewController中创建了一个带有标签和文本字段的原型单元格 每一行都是在运行时创建的,如下所示。当用户单击“保存”按钮时,所有详细信息(如课程详细信息、注册id、用户名和密码)都应保存在相应的变量中。但它不起作用。它在所有变量中存储最后一个文本字段的值 如何获取表中每一行的文本值 //行创建代码 cell variable is global override func tableView(tableView: UITableView, cellForRo
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。如果可能,请给出一个示例。在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 "];
}
在你的应用程序中,4个单元格是固定的?saveButtonClicked中的单元格变量来自哪里?@Chetan Prajapati-yes@deadbeef-抱歉,忘记提供单元格变量是全局变量的信息。那么为什么不使用静态单元格而使用动态单元格呢。bcoz 4单元是可靠的存储器。没什么大不了的。在上面的代码中还有很多错误,我建议您在RegCell中添加一个identifier属性,例如“user_name”或“password”,并将它们用作我上面建议的可变字典中的键。这将从实际显示的标签文本中分离出来。非常好的回答您在@MarkHim上保存了我的一天