目前,我有一个tableview设置了一个tableviewcell的子类。在此tableviewcell上,我有一个显示添加或显示的按钮。我想知道是否存在一种方法来存储按钮相对于其行的状态。例如,我有一个搜索栏以及该表视图,如果我将表视图第四行的按钮的状态更改为从添加中减去,然后从其添加,然后在搜索栏中搜索特定行,它将显示在第一行,但不会保留按钮的状态。我想知道是否有一种方法可以不使用后端(或数据库)。
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)
}
}
}
关于ios - Swift 3.0 TableViewCell:保存按钮的状态并使其保持不变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45496856/