2015-11-02 55 views
2

我在我的iOS應用中使用自定義TableViewCell。我使用方法tableViewDidSelectRowAtIndexPath打開新的ViewController。我需要做的是在自定義單元格的某處添加一個按鈕或圖像,所以如果我點擊按鈕或任何元素都不會打開ViewController,但是在不打開單元的情況下執行某個功能。如何點擊按鈕或圖像,而不用Swift選擇整個單元格?

+0

它可以輕鬆完成分享你還沒有這樣做,我們可以幫助 – baydi

+0

添加您的按鈕的@IBAction不起作用? – Fustigador

+0

使用@IBAction並將標籤設置爲按鈕,然後找出按下的按鈕。 – good4pc

回答

1

set [cell.button setTag:indexPath.row] in cellForRowAtIndexPath method。

,比addTargetcell.button

[cell.button addTarget:self action:@selector(yourAction:) forControlEvents:UIControlEventTouchUpInside]];

,比做任何你想在yourAction

做與發件人的標籤。

或者你想要的代碼比請添加你的代碼你已經做了什麼,所以我們可以幫助更多,如果你是新的iOS。

+0

這就是我需要的。非常感謝!很棒。 –

1

此代碼可以幫助你

在這裏,我使用的自定義BUTTOM表並添加目標到布頓

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 


    let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) 

    // use your custom cell here 



    //cell = UIColor.redColor() 


    //cell.textLabel?!.text = String(data[indexPath.row]) 

    //nameTextField.text = "" 

    let custom_btn : UIButton? = UIButton.init(type: .System) 
    //declaring custom button 

    custom_btn?.setTitle("custom button", forState: .Normal) 

    custom_btn!.tag = indexPath.row 

    custom_btn!.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 

    cell .addSubview(custom_btn!); 

    return cell as! UITableViewCell 
} 



func buttonClicked(sender:UIButton) 
{ 
    if(sender.tag == 5){ 

     //Do something for tag 
    } 
    print("hello") 
} 
相關問題