iOS - UITableViewCell, UIAlertController

标签 ios swift uialertcontroller uitableview

大家好。遇到了问题。我需要制作一个带有按钮的表格,然后单击该按钮,我会收到有关单元格编号的警报。表格单元格本身不活动。我就是这样意识到的。当我一开始滚动表格时一切正常,当您按下按钮时,会显示带有正确行号的警报,但在 4 个元素后出现错误。

此错误出现在我使用 4 标记的行中。 fatal error :在展开可选值时意外发现 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)
}

最佳答案

您为实现此过程而设计的内容不正确。你能做什么

  1. 制作自定义单元格
  2. 在自定义单元格中添加按钮
  3. 在 CellForRowAtIndexPath 中的该按钮中添加操作
  4. 处理来自添加了 tableView 的 ViewController 的操作。

我为你做了一个完整的项目。只是想让你知道。如果你想在 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)


}

只检查三个文件:

Github link

  1. ViewController.swift

  2. sampleTableViewCell.swift

  3. sampleTableViewCell.xib**

这是输出:

enter image description here

关于iOS - UITableViewCell, UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44429411/

相关文章:

ios - 在 Swift 中向通知添加 if 语句

ios - 用于访问 PHAsset 对象的 URL 信息的同步方法

ios - Swift - 解析 Facebook "Invalid redeclaration of ' applicationDidBecomeActive'”

ios - 如何在 swift 的辅助功能中使用点击手势?

swift - PWA 中的额外边距 - 无法拉伸(stretch)整个高度

ios - 带有文本字段验证警报的 UIAlertController 在 swift ios 中显示问题

ios - 如何从 viewController 访问 applicationDidEnterBackground

ios - 关闭模态视图 Controller 呈现的 UIAlertController

swift - 在没有 UITableView 的情况下从 UITableViewCell 中呈现弹出 UIAlertController

ios - 应用程序删除 - APNS 对推送通知失败时对开发服务器的响应