ios - 表格 View 行中的 UISwitch

标签 ios swift uitableview uiswitch

在下面的代码中,我用一些数据填充我的表。开关已关闭,但其实它们并不一定要关闭。在 Storyboard中我将其定义为“开”。

单元格:

var switchHandler: ((Bool)->Void)?

@IBAction func switchChanged(_ sender: UISwitch) {
    self.switchHandler?(sender.isOn)
}

View Controller :

var selectedCells = Set<IndexPath>()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell") as? SmsTableViewCell

    cell?.PhonNumberLbl.text = data![indexPath.section].contacts[indexPath.row]?.phoneNumber
    cell?.NameLbl.text = data![indexPath.section].contacts[indexPath.row]?.name
    cell?.selectedTF.isOn = (data![indexPath.section].contacts[indexPath.row]?.selected)!

    cell?.selectedTF.isOn = self.selectedCells.contains(indexPath)
    cell?.switchHandler = { (switchState) in
        if switchState {
            self.selectedCells.insert(indexPath)
        } else {
            self.selectedCells.remove(indexPath)
        }
    }

    return cell!
}

型号:

typealias smsModelList = [SmsModel]

struct SmsModel:Codable {
    var unitNo:Int?
    var unitPlaque:String?
    var billText:String?
    var contacts:[ContactsModel?]
}

typealias contactlistmodel = [ContactsModel]

struct ContactsModel:Codable
{
    var id :Int?
    var selected :Bool?
    var phoneNumber : String?
    var name : String?
}

有人发现有什么问题导致开关关闭吗?

最佳答案

首先,当您强制解开单元格时,无论如何都要在 dequeue 行中执行此操作,以避免出现不必要的问号,并使用 API 返回非可选单元格

let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell", for: indexPath) as! SmsTableViewCell

要解决您的问题,请直接更新 ContactsModel 结构的 selected 属性,并忘记额外的 selectedCells 数组。进一步声明 - 至少 - selected 为非可选,实际上不存在 maybe 状态。并将所有数据源数组 (data/contacts) 声明为非可选,仅当特定位置存在项目时才调用 cellForRow默认为indexPath。

struct ContactsModel : Codable {
   ...
   var selected : Bool
   ...
}

...

let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell", for: IndexPath) as! SmsTableViewCell
let contact = data[indexPath.section].contacts[indexPath.row]
cell.PhonNumberLbl.text = contact.phoneNumber
cell.NameLbl.text = contact.name
cell.selectedTF.isOn = contact.selected

cell.switchHandler = { [unowned self] switchState in
    // as the structs are value types you have to specify the full reference to the data source array
    self.data[indexPath.section].contacts[indexPath.row].selected = switchState
}

在这种情况下考虑使用类而不是结构,这样就可以缩短闭包

cell.switchHandler = { switchState in
    contact.selected = switchState
}

关于ios - 表格 View 行中的 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52679140/

相关文章:

ios - 带条件的UIPickerView

ios - 在 iOS 中播放视频失败

ios - 将 buttonIndex 与多个警报 View 一起使用

ios - 更改导航栏时平滑过渡 "prefersLargeTitles"

ios - TableView 不会从 Parse.com 重新加载数据

ios - 在 UITableViewCell 中复制 UITextField

iphone - 在主 `UITableViewCell` 已经加载后,如何用 `UIScrollView` 或 `UITableView` 替换 `UITableView`?

ios - 切换按钮静音(快速)

ios - UISearchController 的 init(searchResultsController : UIViewController?) 崩溃

swift - api返回的数据总是nil?