ios - 如何将 UIButtons 添加到我的 TableViewCell 中?

标签 ios swift uitableview uibutton cell

我 14 岁,住在德国,抱歉我的英语不好!

我想将 UIButtons 添加到我的 TableViewCells 中,当点击它们时,它们会调用自己的索引。

我使用 Swift 4。

最佳答案

首先创建 UIButton 到 tableviewCell 类的导出

现在在你的 viewController 中

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourTableViewCell
       cell.bttnName.tag = indexPath.row // The indexPath Row will be stored an button's tag
        cell.bttnName.addTarget(self, action: #selector(selectedButton), for: .touchUpInside)


        return cell
    }

现在为按钮创建一个目标函数(按钮操作)

 @objc func selectedButton(sender: UIButton!) {
        let button = sender!
        print("Clicked Index:",button.tag)
}

只需调用button.tag即可获取您单击的按钮单元格索引

关于ios - 如何将 UIButtons 添加到我的 TableViewCell 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014255/

相关文章:

iphone - 如何在uitableview iphone sdk的cell.contentview上添加 View

ios - 自定义 URL 方案错误 : This app is not allowed to query for scheme

ios - 最小值和最大值函数不能快速工作

ios - 按国家/地区代码将 NSDecimalNumber 格式化为货币,且不损失精度

ios - 隐藏堆栈 View 时,并非所有内容都会在堆栈 View 中进行动画处理

ios - 如何在快速调用 dismissViewController 后调用重新加载 TableView ?

swift - 为什么我的表格 View 动画在 Xcode 9 中如此奇怪?

objective-c - Swift 类使用 Objective-C 类使用 Swift 类

ios - 从未调用过通过 loadNibNamed() 从 xib 加载按钮

ios - 输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView