gpt4 book ai didi

ios - swift 3.0 TableViewCell : Saving the Status of a Button and Having it Persist

转载 作者:行者123 更新时间:2023-11-30 12:15:21 25 4
gpt4 key购买 nike

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

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 : Saving the Status of a Button and Having it Persist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45496856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com