2017-06-02 87 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並重新加載該單個單元格。表視圖有一個方法來更新索引路徑中的單元格... –

相關問題