2017-06-02 88 views
0

我从XIB文件制作一个自定义单元格,并在执行我的tableView, 我能够实现从自定义单元格的文字,但我怎么能实现相同的按钮,就可以接收触摸?如何在自定义单元格中添加按钮?

这里是我写的代码:

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 
} 

pillCustomTableViewCell1,我想在我的细胞,并按下按钮时如何有操作按钮?请帮忙。

+0

采取ref: - > https://stackoverflow.com/questions/28894765/ios-swift-button-action-in-table-view-cell –

+0

有这么多的问题在这段代码我不是知道从哪里开始... ...我建议你找一些教程首先阅读,也许[这](https://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell ),其他问题是你的命名,拿着UI对象结构,arrayOfData有形象,但你的结构不要,用'loadNibName' .... – Tj3n

+0

@ Tj3n创建细胞是的,我不知道如何通过UIButton的,但我知道这就是的UIImage为什么我为UIImage写作,它的工作原理,但我想要一个UIButton –

回答

0

这是从你问的问题的方式,但我相信你正在寻找这样的东西。我希望代码和评论能够说明问题。

// 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 
    } 

} 

尽管如此,我仍然可以做些什么,但随时可以尝试。

+0

谢谢,你的代码就像一个魅力。轻按按钮时是否可以对按钮进行更改?就像它在控制台中发送消息一样。如果我想更改按钮的图像,说明它已被按下,该怎么办? –

+0

@Trikha那么你需要跟踪更改。如果您在数据源中跟踪该数据,我认为最好。所以你要做的是在你的类中添加另一个属性,例如“isPressed”,它默认为false。然后在单元格的行中,您可以添加未按下所需的图像。触发该方法后,您将更改数据源中的属性,如dataSource [sender.tag] .isPressed =!dataSource [sender.tag] .isPressed并重新加载该单个单元格。表视图有一个方法来更新索引路径中的单元格... –

相关问题