OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCell 中的多个按钮在 'top-down' UITableView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:13
标题: ios - UITableViewCell 中的多个按钮在 'top-down' UITableView

我正在尝试解决当表格 View “自上而下”时如何在 UITableViewCell 中操作按钮 - 例如,每个新行都添加到表格 View 的顶部。我通过以下代码实现了这一点:

我使用以下代码将新项目插入到我的模型数组中,然后插入到 tableview 中:

let newItem = Item(text: inputTextView.text, time: Date())
items.insert(newItem, at: 0) //inserting into the front of model array

tableView.beginUpdates()
let indexPath:IndexPath = IndexPath(row:0, section:0)
tableView.insertRows(at: [indexPath], with: .fade)
tableView.endUpdates()

在 cellForRowAt 函数中,我运行以下代码:

let cell = tableView.dequeueReusableCell(withIdentifier: postCellID, for: indexPath) as! NewPostCell
let item = items[indexPath.row]

cell.postTextLabel.text = text
cell.timeLabel.text = dateFormatter.string(from: time)

cell.selectionStyle = .none

return cell

我的每个 tableview 单元格中都有三个按钮。

如何连接这些按钮,以便知道从哪个 indexPath 按下了哪个按钮?

问题是,如果我使用 indexPath.row 来标记按钮,那么所有单元格中的按钮都会被标记为 0,因为每个插入都发生在 indexPath.row 第 0 位的表格顶部。

我想用我的模型数组的当前大小来标记按钮,但这也不起作用,因为当重新使用单元格时,它们可以在那时用数组的长度进行标记,这将是错了。

有很多应用程序在表格 View 类型的设置中具有“顶部的最后一个条目”,并且在单元格中带有按钮。所以一定有办法做到这一点。



Best Answer-推荐答案


您可以将 UIButton 添加到您的 UITableViewCell 并通过标记访问这些 UIButton 并将目标方法添加到这些按钮:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // create a new cell if needed or reuse an old one
        let cell:UITableViewCell = self.tableVw.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell!

        //Access UIButton
        let button1:UIButton = cell.viewWithTag(10) as! UIButton
        let button2:UIButton = cell.viewWithTag(11) as! UIButton
        let button3:UIButton = cell.viewWithTag(12) as! UIButton

        //Add Action Methods to UIButtons
        button1.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
        button2.addTarget(self, action: #selector(SecondButtonClick), for: .touchUpInside)
        button3.addTarget(self, action: #selector(ThirdButtonClick), for: .touchUpInside)

        return cell
    }

按钮操作

// MARK: - UIButton Methods.
    func FisrtButtonClick(_ sender: Any)  {
        //Get Button cell position.
        let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
        let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
        if indexPath != nil {
            print("Cell indexPath: \(indexPath?.row)")
        }
    }

    func SecondButtonClick(_ sender: Any)  {

        //Get Button cell position.
        let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
        let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
        if indexPath != nil {
            print("Cell indexPath: \(indexPath?.row)")
        }

    }

    func ThirdButtonClick(_ sender: Any)  {

        //Get Button cell position.
        let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
        let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
        if indexPath != nil {
            print("Cell indexPath: \(indexPath?.row)")
        }

    }

关于ios - UITableViewCell 中的多个按钮在 'top-down' UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015157/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4