Swift 3.0 TableViewCell:保存按钮的状态并使其持久化



目前,我有一个表视图设置了一个表视图单元格的子类化。在这个表视图单元格上,我有一个显示添加或显示的按钮。我想知道是否有办法存储按钮相对于其行的状态。例如,我有一个搜索栏和这个表格视图,如果我更改了表格视图第 4 行按钮的状态以从 add 中减去,然后在搜索栏中搜索特定行,它将显示在第一行,但不会保留按钮的状态。我想知道是否有办法在不使用后端(或数据库(的情况下做到这一点。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = data.filter({ (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if (filtered.count == 0){
searchActive = false
} else {
searchActive = true
}
self.TableView.reloadData()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive == true {
return filtered.count
}
return data.count
}
var status = [IndexPath: Bool]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ListCell
cell.cellDelegate = self
cell.contentView.bringSubview(toFront: cell.Button)

if status[indexPath] ?? false {
cell.Button.setTitle("Subtract", for: .normal)
} else {
cell.Button.setTitle("Add", for: .normal)
}
cell.indexPath = indexPath
if(searchActive) {
cell.textLabel?.text = filtered[indexPath.row]
} else {
cell.textLabel?.text = data[indexPath.row]
}
cell.contentView.bringSubview(toFront: cell.Button)
return cell
}
func didPressButton(indexPath: IndexPath) {
guard let cell = TableView.cellForRow(at: indexPath) as? ListCell else {
return
}
if status[indexPath] ?? false {
status[indexPath] = false
cell.Button.setTitle("Add", for: .normal)
} else {
status[indexPath] = true
cell.Button.setTitle("Subtract", for: .normal)
}
}

感谢您指导我。这是我拥有的工作。在我的数据数组中,我在每个项目中添加了另一个唯一的条目(A,B,C(。然后我使用 我的状态数组中的此键值,用于确定是否已添加它。请让我知道是否有更有效的方法。

var data = [["Apples","A"],["Bananas","B"],["Corn","C"],["Doughnuts","D"],["Eggplant","E"],["Flour","F"]]
var filtered: [[String]] = []
var searchActive : Bool = false 
var status = [String: Bool]()
var name : String = String()
var filteredName : String = String()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = data.filter({ (text) -> Bool in
let tmp: NSString = text[0] as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if (filtered.count == 0){
searchActive = false
} else {
searchActive = true
}
self.guestTableView.reloadData()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")  
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive == true {
return filtered.count
}
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! GuestListCell
cell.cellDelegate = self
cell.contentView.bringSubview(toFront: cell.button)

item = data[indexPath.row][1]

if status[item] ?? false {
cell.button.setTitle("Subtract", for: .normal)
} else {
cell.button.setTitle("Add", for: .normal)
}
cell.indexPath = indexPath
if(searchActive) {
cell.textLabel?.text = filtered[indexPath.row][0]
if status[filtered[indexPath.row][1]] ?? false {
cell.button.setTitle("Subtract", for: .normal)
} else {
cell.button.setTitle("Add", for: .normal)
}
} else {
filteredName = data[indexPath.row][1]
cell.textLabel?.text = data[indexPath.row][0]
}
cell.contentView.bringSubview(toFront: cell.button)
return cell
}
func didPressButton(indexPath: IndexPath) {
guard let cell = guestTableView.cellForRow(at: indexPath) as? GuestListCell else {
return
}
if searchActive {
if status[filtered[indexPath.row][1]] ?? false {
print("the value of searchActive is (searchActive)")
status[filtered[indexPath.row][1]] = false
cell.button.setTitle("Add", for: .normal)
} else {
status[filtered[indexPath.row][1]] = true
cell.button.setTitle("Subtract", for: .normal)
}
} else {
if status[data[indexPath.row][1]] ?? false {
status[data[indexPath.row][1]] = false
cell.button.setTitle("Add", for: .normal)
} else {
status[data[indexPath.row][1]] = true
cell.button.setTitle("Subtract", for: .normal)
}
}
}

最新更新