[英]Tableview cell multiple check mark selection to add cell data into single array Swift 4.2?
My scenario, I have loaded my JSON
data into tableView
with help of codable
. 我的场景是,我在
codable
帮助下将JSON
数据加载到tableView
中。 Here, I have added my tableView
cell multiple
check mark select and deselect
. 在这里,我添加了
tableView
单元格multiple
复选标记select和deselect
。 Now, If I am selecting tableView cell I can able to get cell
data but I want to add within one array
, same if I am unselecting cell It should remove
from the array. 现在,如果我选择tableView单元格,我可以获取
cell
数据,但是我想在一个array
内添加,如果我取消选择单元格,则同样如此。它应该从数组中remove
。 Selected cell data I am moving to another ViewController
. 选定的单元格数据我要移到另一个
ViewController
。 I would like to know how to do that. 我想知道该怎么做。
My Code 我的密码
// 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
}
}
}
My Cell Selection current output 我的单元格选择当前输出
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”))
You can try 你可以试试
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)
}
}
}
Then inside cellForRowAt
然后在
cellForRowAt
内部
let cell = ////
let item = users[indexPath.row]
cell.accessoryType = selectedArr.contains(item) ? .checkmark : .none
Also make sure model named Item
conforms to Equatable
还要确保名为
Item
模型符合Equatable
given 特定
for (index, element) in item.enumerated() {
print("Item \(index): \(element)")
}
that gives 这给
Item 0: 121, Item 1: 122, Item 2: 123
项目0:121,项目1:122,项目2:123
Then it's an array of Ints , so do 然后是一个Ints数组,所以
let res = item.map{ "\($0)" }.joined(separator: ",")
So it sounds like you want to be able to have an array that contains all of the selected Users. 因此,听起来您希望能够拥有一个包含所有选定用户的数组。 Then what you would do is have an array like this instantiated in the class declaration:
然后,您将要做的是在类声明中实例化一个这样的数组:
var users:[Users] = []
Now what you should be doing is leveraging swift's protocols in order to handle the heavy lifting for you; 现在,您应该做的就是利用swift的协议来为您处理繁重的工作; meaning, when removing a previously selected user from the Users array, you shouldn't need a for loop, but something more familiar: contains.
意思是,当从Users数组中删除先前选择的用户时,您不需要for循环,而是需要更熟悉的内容:包含。
extension User: Equatable {
static func == (lhs: User, rhs: User) -> Bool {
return lhs.userId == rhs.userId
}
}
So now you can call this when removing or adding, for example: 因此,现在您可以在删除或添加时调用它,例如:
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)
}
Don't use an extra array, add the selected information to your model. 不要使用额外的数组,将所选信息添加到模型中。
struct User : Codable {
var isSelected = false
// ... other members
}
Assuming the data source array is declared as users
, set the checkmark depending on the isSelected
value in cellForRowAt
假设数据源数组声明为
users
,则根据cellForRowAt
的isSelected
值设置选中标记
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
...
}
In didSelectRowAt
just toggle isSelected
and reload the row 在
didSelectRowAt
只需切换isSelected
并重新加载该行
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
users[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none)
}
To get all selected users just filter
the array 要获取所有选定的用户,只需
filter
数组
let selectedUsers = users.filter{ $0.isSelected }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.