2017-12-06 5 views
0

私はUISearchControllerを持っています。UITableViewControllersearchResultsControllerです。UITableViewCellのアクセサリービューのカスタムボタンからインデックスパスを取得

class SearchResultsViewController: UITableViewController { 

    var fruits: [String] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.tableFooterView = UIView(frame: .zero) 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    } 

    @objc func addFruit(_ sender: UIButton) { 
     let point = tableView.convert(sender.bounds.origin, to: sender) 
     let indexPath = tableView.indexPathForRow(at: point) 
     print(indexPath?.row) 
    } 

    // MARK: - Table view data source 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return fruits.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
     cell.textLabel?.text = fruits[indexPath.row] 
     cell.selectionStyle = .none 

     let addButton = UIButton(type: .custom) 
     addButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44) 
     addButton.setImage(UIImage(named: "add"), for: .normal) 
     addButton.contentMode = .scaleAspectFit 
     addButton.addTarget(self, action: #selector(addFruit(_:)), for: .touchUpInside) 
     addButton.sizeToFit() 
     cell.accessoryView = addButton 

     return cell 
    } 

} 

検索結果が表示されるセルにカスタムボタンを表示する必要があります。だから私はを細胞 'accessoryViewとして追加しました。それは見た目とうまく動作します。

enter image description here

今私は、ユーザーがこのボタンをタップしたときに、セルのindexPathを取得する必要があります。

私はそれを以下のように取得しようとしています。

@objc func addFruit(_ sender: UIButton) { 
    let point = tableView.convert(sender.bounds.origin, to: sender) 
    let indexPath = tableView.indexPathForRow(at: point) 
} 

しかし、それはすべてのセルに対してnilを返し続けます。

カスタムボタンタップからindexPathを取得する他の方法はありますか?私はここでもdemo projectを追加しました。

+0

try 'let cell = sender.superview?.superview as? CustomTableViewCell let indexPath = tblView.indexPath(for:cell!) ' –

答えて

1

SOButtonという名前のカスタムクラスを作成し、タイプIndexPathの変数を追加します。追加ボタンの初期化にこのクラスを使用します。

//Your class will look like - 

class SOButton: UIButton { 
    var indexPath: IndexPath? 
} 

//Your action will look like - 

@objc func addFruit(_ sender: SOButton) { 
    print(sender?.indexPath.row) 
} 

//And in your cellForRow add 

let addButton = SOButton(type: .custom) 
addButton.indexPath = indexPath 

希望、これはあなたを助け:)

+0

これは機能します!ありがとうございました。 – Isuru

+0

HappyToHelp !!! :) –

0
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView) 
let currentIndexPath = self.tableView.indexPathForRow(at: buttonPosition) 

それは私のために働いたこれを試してみてください:)

0

私はこの光ソリューションを使用することをお勧めします:buttonタグにindexPath.rowを追加します。

let addButton = UIButton(type: .custom) 
addButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44) 
addButton.tag = indexPath.row 

ボタン操作:

@objc func addFruit(_ sender: UIButton) { 
    print(sender.tag) 
} 
0

コードを更新してください。変換関数でUIButtonの送信者を渡します。それらにtableViewを渡してください。

func getIndexPathByCgPoint(_ sender: UIButton) -> IndexPath? { 
    let point = sender.convert(sender.bounds.origin, to: tableview) 
    guard let indexPath = tableview.indexPathForRow(at: point) else { 
     return nil 
    } 
    return indexPath 
} 

ただし、セクションヘッダーの場合はnilを返します。

関連する問題