You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中使用M13CheckBox Pod获取选中行的方法咨询

Solution for Getting Selected Row with M13CheckBox from Storyboard Prototype Cell

Hey there! I’ve dealt with this exact issue using M13CheckBox before—since it doesn’t come with built-in delegate methods, we need a workaround to track which row’s checkbox was tapped. Here are two reliable approaches:

Approach 1: Use Closure Callbacks in Custom Cell

This is my go-to method because it keeps your code clean and decoupled.

First, create a custom UITableViewCell subclass linked to your storyboard prototype:

import UIKit
import M13Checkbox

class CheckBoxTableViewCell: UITableViewCell {
    @IBOutlet weak var checkBox: M13Checkbox!
    
    // Add a closure to pass tap events and index path back to your view controller
    var checkboxTapHandler: ((Bool, IndexPath) -> Void)?
    var currentIndexPath: IndexPath?
    
    @IBAction func onCheckBoxTapped(_ sender: M13Checkbox) {
        guard let indexPath = currentIndexPath else { return }
        let isChecked = sender.checkState == .checked
        checkboxTapHandler?(isChecked, indexPath)
    }
}

Then, in your view controller’s tableView(_:cellForRowAt:) method, set up the closure and index path:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CheckBoxCell", for: indexPath) as! CheckBoxTableViewCell
    
    // Pass the current index path to the cell
    cell.currentIndexPath = indexPath
    
    // Configure the checkbox's initial state from your data source
    let item = yourDataSource[indexPath.row]
    cell.checkBox.checkState = item.isChecked ? .checked : .unchecked
    
    // Set up the closure to handle tap events
    cell.checkboxTapHandler = { [weak self] isChecked, indexPath in
        // Update your data source with the new state
        self?.yourDataSource[indexPath.row].isChecked = isChecked
        // Do anything else you need (e.g., update UI, trigger actions)
        print("Row \(indexPath.row) was \(isChecked ? "checked" : "unchecked")")
    }
    
    return cell
}

Approach 2: Traverse the View Hierarchy to Find the Cell

If you prefer not to use closures, you can find the parent cell directly from the tapped checkbox:

In your view controller, link the checkbox’s action to this method:

@IBAction func checkBoxTapped(_ sender: M13Checkbox) {
    // Traverse up the view hierarchy to find the parent table view cell
    var currentView = sender.superview
    while let view = currentView, !(view is UITableViewCell) {
        currentView = view.superview
    }
    
    guard let cell = currentView as? UITableViewCell,
          let indexPath = tableView.indexPath(for: cell) else { return }
    
    let isChecked = sender.checkState == .checked
    // Update your data source and handle the selection
    yourDataSource[indexPath.row].isChecked = isChecked
    print("Selected row: \(indexPath.row), state: \(isChecked)")
}

Notes:

  • Always make sure to update your data source when the checkbox state changes—this ensures your table view reflects the correct state if cells are reused.
  • Approach 1 is more scalable and easier to maintain, especially if you have complex cell logic.

内容的提问来源于stack exchange,提问作者Tarsha de Souza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:02:33