【发布时间】: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