2017-09-23 10 views
0

셀에 단추가 있고이 단추는 고유 ID가있는보기를 열어야한다는 표보기가 있습니다. 그래서 내 단추에 인수를 전달해야하지만 addTarget 속성을 사용하면 매개 변수없이 함수를 호출 할 수 있습니다.표보기 셀에 단추 대상 추가

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
... 
    cell.editButton.addTarget(self, action: #selector(goToEdit(id:)), for: .touchUpInside) 
} 

func goToEdit(id: String) { 
    let edit = EditAdViewController(editingAdId: id) 
    self.navigationController?.pushViewController(edit, animated: true) 
} 

일부 매개 변수가 포함 된 동작을 버튼으로 참조 할 수있는 방법이 있습니까? 모두 감사합니다 :)

+0

https://stackoverflow.com/a/46379494/3746301 – Shades

답변

0

아마도 @IBAction에 버튼을 연결하고 params [indexPath.row]를 사용해 볼 수 있습니다.

var cell = sender.superview() as? UITableViewCell 
var indexPath: IndexPath? = yourTableView.indexPath(for: cell!) 
+0

불행하게도 #selector는 –

+0

가 @IBAction 및 사용에 당신이 버튼을 연결하려고 어떤 매개 변수를 얻을 수 없다 params [indexPath.row]입니다. indexPath를 얻으려면 : var cell = sender.superview() as? UITableViewCell var indexPath : IndexPath? = yourTableView.indexPath (for : cell!) –

+0

@LucasMoraes 주석에 코드를 게시하지 마십시오. [편집] 귀하의 답변은 모든 관련 세부 사항입니다. – rmaddy

0

당신은 사용자 정의있는 UITableViewCell에 위임 기능을 추가하는 시도 할 수 있습니다 :

는 indexPath를 얻으려면.

예를 들어,이 정의 tableViewCell 내부 버튼이 : 나는의 위임 기능을 구현하는 UITableViewDataSource (cellForRow)를 통해 물론 내 컨트롤러를 준수에

import UIKit 

protocol PickupTableViewCellDelegate: NSObjectProtocol { 
    func pickupTableViewCell(userDidTapPickup pickup: Pickup, pickupTableViewCell: PickupTableViewCell) 
} 

class PickupTableViewCell: UITableViewCell { 

    // MARK: - Properties 

    @IBOutlet private weak var label_UserFullName: UILabel! 
    .... 

    // MARK: - Functions 
    // MARK: IBAction 

    @IBAction func pickup(_ sender: Any) { 
     self.delegate?.pickupTableViewCell(userDidTapPickup: self.pickup, pickupTableViewCell: self) 
    } 
} 

그런

PickupTableViewCell.swift을 내 tableViewCell.

HomeViewController.swift

// MARK: - UITableViewDataSource 

extension HomeViewController: UITableViewDataSource { 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let pickupTVC = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.pickupTableViewCell)! 
     pickupTVC.delegate = self 
     pickupTVC.pickup = self.pickups[indexPath.section] 

     return pickupTVC 
    } 
} 

// MARK: - PickupTableViewCellDelegate 

extension HomeViewController: PickupTableViewCellDelegate { 
    func pickupTableViewCell(userDidTapPickup pickup: Pickup, pickupTableViewCell: PickupTableViewCell) { 
     // Do something 
    } 
}