繁体   English   中英

Tableview单元格多个复选标记选择以将单元格数据添加到单个数组中Swift 4.2?

[英]Tableview cell multiple check mark selection to add cell data into single array Swift 4.2?

我的场景是,我在codable帮助下将JSON数据加载到tableView中。 在这里,我添加了tableView单元格multiple复选标记select和deselect 现在,如果我选择tableView单元格,我可以获取cell数据,但是我想在一个array内添加,如果我取消选择单元格,则同样如此。它应该从数组中remove 选定的单元格数据我要移到另一个ViewController 我想知道该怎么做。

我的密码

// method to run when table view cell is tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number \(indexPath.row).")

        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            if cell.accessoryType == .checkmark {
                cell.accessoryType = .none
            } else {
                cell.accessoryType = .checkmark
                let item = users[indexPath.row]
                print(item) // here printing cell selection data 
            }
        }
    }

我的单元格选择当前输出

You tapped cell number 1.
User(userId: "121”, active: 1, name: example_table.Name(firstname: "jack", lastname: "m"))
You tapped cell number 2.
User(userId: "122”, active: 1, name: example_table.Name(firstname: “rose”, lastname: “h”))
You tapped cell number 3.
User(userId: "123”, active: 1, name: example_table.Name(firstname: “makj”, lastname: “i”))

你可以试试

var selectedArr = [Item]()
var users = [Item]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.row).")
    let item = users[indexPath.row]
    if let cell = tableView.cellForRow(at: indexPath) { // you can also omit the if let and force-unwrap as in this case cell will never be nil 
        if cell.accessoryType == .checkmark {
            cell.accessoryType =  .none
            selectedArr.remove(where:{ $0 == item })
        } else {
            cell.accessoryType = .checkmark
            selectedArr.append(item)
        }
    }
}

然后在cellForRowAt内部

let cell = ////
let item = users[indexPath.row]
cell.accessoryType = selectedArr.contains(item) ? .checkmark : .none

还要确保名为Item模型符合Equatable


特定

  for (index, element) in item.enumerated() { 
     print("Item \(index): \(element)")
  }

这给

项目0:121,项目1:122,项目2:123

然后是一个Ints数组,所以

 let res = item.map{ "\($0)" }.joined(separator: ",")

因此,听起来您希望能够拥有一个包含所有选定用户的数组。 然后,您将要做的是在类声明中实例化一个这样的数组:

var users:[Users] = []

现在,您应该做的就是利用swift的协议来为您处理繁重的工作; 意思是,当从Users数组中删除先前选择的用户时,您不需要for循环,而是需要更熟悉的内容:包含。

extension User: Equatable {
    static func == (lhs: User, rhs: User) -> Bool {
        return lhs.userId == rhs.userId
    }
}

因此,现在您可以在删除或添加时调用它,例如:

var thisUser = User(userId: "123”, active: 1, name: example_table.Name(firstname: “makj”, lastname: “i”))

if users.contains(thisUser) {
    users.removeAtIndex(users.indexOf(thisUser))
} else {
    //Add the user to the array here for example
    users.append(thisUser)
}

不要使用额外的数组,将所选信息添加到模型中。

struct User : Codable {

    var isSelected = false
    // ... other members

}

假设数据源数组声明为users ,则根据cellForRowAtisSelected值设置选中标记

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let user = users[indexPath.row]
    cell.accessoryType = user.isSelected ? .checkmark : .none

    ... 
}

didSelectRowAt只需切换isSelected并重新加载该行

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    users[indexPath.row].isSelected.toggle()
    tableView.reloadRows(at: [indexPath], with: .none)
}

要获取所有选定的用户,只需filter数组

let selectedUsers = users.filter{ $0.isSelected }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM