[英]Button in Custom cell of table view
我在tableView的自定義單元格中有一個按鈕。 對於不同的行,所有按鈕的功能都不同。 但是由於我有自定義單元格,因此當我按下按鈕時,它不會告訴您選擇了哪一行,它會運行該按鈕的默認功能。 任何人都可以幫助我如何單擊按鈕來更改功能。 或如何知道選擇了哪個行按鈕。
您可以讓按鈕處理程序檢查單元格的indexPath.row
。 這就是您知道按鈕包含在哪個單元格中的方式。
在您的cellForRowAtIndexPath中使用;
cell.yourbutton.tag = indexPath.row
cell.yourbutton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
然后為您的按鈕創建一個函數
func buttonPressed(sender: UIButton) {
var buttonCell = sender.tag
// do what you need
// This will do what you need for the selected button in the cell
}
您可以通過在表視圖上調用indexPathForSelectedRow()
或indexPathsForSelectedRows()
方法來判斷選擇了哪個單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.