简体   繁体   English

如何在自定义单元格中添加按钮?

[英]How can I add a button in a custom cell?

I'm making a custom cell from XIB file and implementing in my tableView , I am able to implement the text from the custom cell but how can I implement the same for the button and receive touches on it ? 我正在从XIB文件制作一个自定义单元格并在我的tableView实现,我能够实现自定义单元格中的文本,但是如何为按钮实现相同的功能并接收触摸呢?

Here is the code I wrote: 这是我写的代码:

struct cellData {

let cell : Int!
let text : String!
let button =  UIButton()
}

var arrayOfData = [cellData]()

override func viewDidLoad() {
    super.viewDidLoad()

arrayOfData = [cellData(cell : 1, text: "ahfhasdf", button: button)]
 }

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

    if arrayOfData[indexPath.row].cell==1{

        let cell=Bundle.main.loadNibNamed("CustomTableViewCell1", owner: self, options: nil)?.first as! CustomTableViewCell1

        cell.label1.text=arrayOfData[indexPath.row].text
        cell.pillImage.currentImage=arrayOfData[indexPath].pillImage

        return cell
}

My pill is the button in CustomTableViewCell1 which I want in my cell and how to have actions when the button is pressed ? 我的pillCustomTableViewCell1中的按钮,我想要在我的单元格中以及按下按钮时如何操作? Please help. 请帮忙。

This is way off from what you are asking in your question but I believe you are looking for something like this. 这与您提出的问题有所不同,但我相信您正在寻找类似的东西。 I hope the code and comments speak for themselves. 我希望代码和评论能够说明问题。

// My cell from xib (storyboard)
class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var cellImageView: UIImageView!
    @IBOutlet weak var label: UILabel!

}

// My view controlle from xib (storyboard)
class MyViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    fileprivate var dataSource: [CellData] = [CellData]()

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = [CellData(index: 0, title: "First cell", image: nil, target: self, selector: #selector(firstCellPressed)),
                      CellData(index: 1, title: "Another cell", image: nil, target: self, selector: #selector(anyOtherCellPressed))]
        tableView.reloadData()
    }

    @objc private func firstCellPressed() {
        print("First cell pressed")
    }

    @objc private func anyOtherCellPressed(sender: UIButton) {
        print("Cell at row \(sender.tag) pressed")
    }

}

fileprivate extension MyViewController {

    struct CellData {
        let index: Int
        let title: String?
        let image: UIImage?
        let target: Any?
        let selector: Selector
    }

}

extension MyViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier", for: indexPath) as! MyTableViewCell // Dequeue cell
        let source = dataSource[indexPath.row] // Get the source object
        cell.button.tag = indexPath.row // Assign tag so we can use it in the button action method

        // Assign target and selector if it exists
        if let target = source.target {
            cell.button.addTarget(target, action: source.selector, for: .touchUpInside)
        }
        cell.cellImageView.image = source.image // Assign image
        cell.label.text = source.title // Assign title
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

}

Still this is far from what I would do but feel free to experiment. 这仍然远不是我想做的事情,而是随意尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何单击UI表格视图单元格(自定义单元格)内的按钮,然后转到其他ViewController - how can I click button inside UI table view cell (custom cell ) and then goto other viewcontroller 如何将按钮图标添加到自定义键盘 iOS 8? - How can I add button icons to custom keyboard iOS 8? 如何为每个单元格添加一个按钮? - How do I add a button to each cell? 如何在自定义单元格中居中放置图像 - How can I center image in custom cell 我可以仅在UITableView中的一个单元格中添加显示按钮吗? - Can I add a disclosure Button to only one cell in my UITableView? 如何在一个自定义tableView单元格中使用两个按钮? 我想在单击一个按钮时更改两个图标 - How can I use two buttons in one custom tableView cell ? I want to when clicked in one button change the icons of both of them 如何在单击按钮时将ImageView添加到表格单元格中? - How do I add an ImageView into a table cell on button click? 如何使用文本框和按钮将文本添加到每个单元格中? - How do I add texts into each cell using textfield and button? 如何向 uitableview 的每个单元格添加一个按钮? - How do I add a button to each cell of the uitableview? 如何在单元格的按钮上添加徽章? - How to add badge on a button in a cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM