我正在使用具有动态行数的uitableview。 (最多4个)。我正在手动进行本地化。所以,当有一个细胞定位工作完美。但是当有多个单元格时我的应用程序崩溃了。 这是我的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : customerCell = tableView.dequeueReusableCellWithIdentifier("customerCell") as! customerCell
func localcell()
{
cell.lblpassegernumber.text = Localization("Passanger1")
cell.lblSeatNo.text = Localization("SeatNo")
cell.lblTCnumberL.text = Localization("TcNumber")
cell.lbltelephonenoL.text = Localization("MobileNumer")
cell.lblnameL.text = Localization("NameSurname")
cell.lblemailL.text = Localization("EMailAddress")
}
if (indexPath.row == 1)
{
localcell()
email1 = txtemailaddress.text
telephone1 = txtnameSurname.text
tcnumber1 = txtTCnumber.text
name1 = txtnameSurname.text
}
if (indexPath.row == 2)
{
localcell()
email2 = txtemailaddress.text!
telephone2 = txtnameSurname.text
tcnumber2 = txtTCnumber.text
name2 = txtnameSurname.text
}
if (indexPath.row == 3)
{
localcell()
email3 = txtemailaddress.text!
telephone3 = txtnameSurname.text
tcnumber3 = txtTCnumber.text
name3 = txtnameSurname.text
}
if (indexPath.row == 4)
{
localcell()
email4 = txtemailaddress.text!
telephone4 = txtnameSurname.text
tcnumber4 = txtTCnumber.text
name4 = txtnameSurname.text
}
return cell
}
我哪里出错了。并且还建议我是否有任何其他选项来访问索引路径中的行的侧面单元格的uitableviewcell元素
答案 0 :(得分:0)
你可以在你的故事板中创建一个原型单元。在你的表视图控制器中这样做
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dynamicnum.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : customerCell = tableView.dequeueReusableCellWithIdentifier("customerCell") as! customerCell
cell.lblpassegernumber.text = Localization("Passanger1")
cell.lblSeatNo.text = Localization("SeatNo")
cell.lblTCnumberL.text = Localization("TcNumber")
cell.lbltelephonenoL.text = Localization("MobileNumer")
cell.lblnameL.text = Localization("NameSurname")
cell.lblemailL.text = Localization("EMailAddress")
return cell
}
答案 1 :(得分:0)
第一行indexPath.row
为0
,而不是1
。所以使用这个:
switch indexPath.row {
case 0:
// handle first row
case 1:
// handle second row
case 2:
// handle third row
case 3:
// handle fourth row
default:
break
}
答案 2 :(得分:0)
我认为你在你的代码中犯了一些错误,我也没有得到正是在做什么
{
localcell()
email1 = txtemailaddress.text
telephone1 = txtnameSurname.text
tcnumber1 = txtTCnumber.text
name1 = txtnameSurname.text
}
我能为您做的是,我可以编辑您的代码,如下所示。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : customerCell = tableView.dequeueReusableCellWithIdentifier("customerCell") as! customerCell
//This is common for all cells so no need to create and call func for this
cell.lblpassegernumber.text = Localization("Passanger1")
cell.lblSeatNo.text = Localization("SeatNo")
cell.lblTCnumberL.text = Localization("TcNumber")
cell.lbltelephonenoL.text = Localization("MobileNumer")
cell.lblnameL.text = Localization("NameSurname")
cell.lblemailL.text = Localization("EMailAddress")
//You were missing indexPath 0 here (not getting what you are doing by this, you could create an array and save dictionary of these values.)
if (indexPath.row == 0) {
email1 = txtemailaddress.text
telephone1 = txtnameSurname.text
tcnumber1 = txtTCnumber.text
name1 = txtnameSurname.text
} else if (indexPath.row == 1) {
email2 = txtemailaddress.text!
telephone2 = txtnameSurname.text
tcnumber2 = txtTCnumber.text
name2 = txtnameSurname.text
} else if (indexPath.row == 2) {
email3 = txtemailaddress.text!
telephone3 = txtnameSurname.text
tcnumber3 = txtTCnumber.text
name3 = txtnameSurname.text
} else if (indexPath.row == 3) {
email4 = txtemailaddress.text!
telephone4 = txtnameSurname.text
tcnumber4 = txtTCnumber.text
name4 = txtnameSurname.text
}
return cell
}
如果要访问外部的单元格元素,可以这样做
如果您需要访问单元格上按钮操作中的单元格元素
@IBAction func buttonClicked(发件人:UIButton) {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableViewOutlet];
NSIndexPath * indexPath = [tableViewOutlet indexPathForRowAtPoint:buttonPosition]; 让thisCell = tableView.cellForRowAtIndexPath(indexPath) }
如果要访问可见的单元格的子视图
let thisCell = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: 0, inSection: 0))
答案 3 :(得分:0)
哦.. Finalyy发现了这个问题。 email1 = txtemailaddress.text
因为我声明email1为complsary意味着不是nil。但是当页面加载文本框为空时,我尝试将nil值保存到email1,这就是问题所在。终于解决了。