LoginSignup
12
10

More than 5 years have passed since last update.

RxSwift UITableViewCell上に配置したボタンの動作

Last updated at Posted at 2018-08-23

UITableView上に配置したボタンの動作がおかしい場合

反応しない、二重に処理が走ってしまうなど

disposedするDisposeBagオブジェクトをセル内で生成したものを利用する

大抵これで解決します。
disposeBagオブジェクトはセルの再利用のたびに新しく生成する必要があります。

セル側の例

class MyTableViewCell: UITableViewCell {
   @IBOutlet weak var replyButton: UIButton!
   var disposeBag = DisposeBag()

    override func prepareForReuse() {
        super.prepareForReuse()
        self.disposeBag = DisposeBag() // ここで毎回生成
    }

ビューコントローラー側

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCell(withIdentifier: String(describing: MyTableViewCell.self), for: indexPath) as! MyTableViewCell

        cell.talkButton.rx.tap.asDriver().drive(onNext: { [weak self] _ in
            // タップ時の処理
        }).disposed(by: cell.disposeBag) // セルで生成したDisposeBagオブジェクト
    return cell
   }
12
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10