iOS - UITableViewCell、UIAlertController
iOS - UITableViewCell, UIAlertController
大家好。遇到了问题。我需要用一个按钮制作一个 table ,然后通过单击该按钮我会收到有关单元格编号的警报。 table 细胞本身不活跃。我就是这样意识到的。当我一开始滚动 table 时一切正常,当您按下按钮时,会显示带有正确行号的警报,但在 4 个元素后出现错误。
此错误出现在我使用 4 标记的行中。
致命错误:在展开可选值
时意外发现 nil
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as UITableViewCell
if (tableView.tag == 1) {
let numLabel: UILabel = tableView.viewWithTag(3) as! UILabel
numLabel.text = String(indexPath.row)
} else if (tableView.tag == 2) {
//Error appears here
let numButton: UIButton = tableView.viewWithTag(4) as! UIButton
numButton.setTitle(String(indexPath.row), for: .normal)
numButton.tag = indexPath.row
}
return cell
}
@IBAction func showAlertForRow(row: UIButton) {
let alert = UIAlertController(title:"Test work",message:"Cell at row \(row.tag) was tapped!",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
您为实现此过程而设计的内容不正确。你能做什么
- 制作自定义单元格
- 在自定义单元格中添加按钮
- 在 CellForRowAtIndexPath 中的该按钮中添加操作
- 处理来自 ViewController 添加 tableView 的操作。
我为 you.Just 做了一个完整的项目让你知道。如果你想在 tableView 中添加 customCell,你需要像这样在 viewDidLoad 中注册它。
我在 ViewController.swift 文件中完成了。查看我的项目。
let nib = UINib.init(nibName:String(describing: sampleTableViewCell.self) , bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "chatCell")
然后检查 cellForRowAtIndexPath 函数:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! sampleTableViewCell
cell.clickMeBtn.tag = indexPath.row
cell.clickMeBtn.addTarget(self, action: #selector(onButtonPressed(sender :)), for: .touchUpInside)
return cell
}
按键功能:
func onButtonPressed(sender:UIButton) {
let alert = UIAlertController.init(title:"Cell index is"+String(sender.tag), message: nil, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction.init(title: "ok", style: UIAlertActionStyle.default) { (UIAlertAction) in
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
只检查三个文件:
ViewController.swift
sampleTableViewCell.swift
sampleTableViewCell.xib**
这是输出:
大家好。遇到了问题。我需要用一个按钮制作一个 table ,然后通过单击该按钮我会收到有关单元格编号的警报。 table 细胞本身不活跃。我就是这样意识到的。当我一开始滚动 table 时一切正常,当您按下按钮时,会显示带有正确行号的警报,但在 4 个元素后出现错误。
此错误出现在我使用 4 标记的行中。 致命错误:在展开可选值
时意外发现 nilfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as UITableViewCell
if (tableView.tag == 1) {
let numLabel: UILabel = tableView.viewWithTag(3) as! UILabel
numLabel.text = String(indexPath.row)
} else if (tableView.tag == 2) {
//Error appears here
let numButton: UIButton = tableView.viewWithTag(4) as! UIButton
numButton.setTitle(String(indexPath.row), for: .normal)
numButton.tag = indexPath.row
}
return cell
}
@IBAction func showAlertForRow(row: UIButton) {
let alert = UIAlertController(title:"Test work",message:"Cell at row \(row.tag) was tapped!",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
您为实现此过程而设计的内容不正确。你能做什么
- 制作自定义单元格
- 在自定义单元格中添加按钮
- 在 CellForRowAtIndexPath 中的该按钮中添加操作
- 处理来自 ViewController 添加 tableView 的操作。
我为 you.Just 做了一个完整的项目让你知道。如果你想在 tableView 中添加 customCell,你需要像这样在 viewDidLoad 中注册它。 我在 ViewController.swift 文件中完成了。查看我的项目。
let nib = UINib.init(nibName:String(describing: sampleTableViewCell.self) , bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "chatCell")
然后检查 cellForRowAtIndexPath 函数:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! sampleTableViewCell
cell.clickMeBtn.tag = indexPath.row
cell.clickMeBtn.addTarget(self, action: #selector(onButtonPressed(sender :)), for: .touchUpInside)
return cell
}
按键功能:
func onButtonPressed(sender:UIButton) {
let alert = UIAlertController.init(title:"Cell index is"+String(sender.tag), message: nil, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction.init(title: "ok", style: UIAlertActionStyle.default) { (UIAlertAction) in
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
只检查三个文件:
ViewController.swift
sampleTableViewCell.swift
sampleTableViewCell.xib**
这是输出: