gpt4 book ai didi

ios - 按单元格文本字符串值删除特定数组元素[swift 4]

转载 作者:可可西里 更新时间:2023-11-01 00:38:43 25 4
gpt4 key购买 nike

我的 UITableView 有两个数组。一个保存数组项,另一个保存数组项的值,以防它们上面有复选标记。我现在遇到了一个问题,因为我的两个数组没有相同的 IndexPath。我需要一些东西来通过字符串值删除我的 selectedChecklist 数组中的项目。我该怎么做?

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
checklist.remove(at: indexPath.row)
selectedChecklist.removeAll { $0 == String(cell.textLabel) }
myTableView.reloadData()
}
}

打印选定的 list

["Test", "Test2", "Test3", "Asdf", "Test2", "Test2", "Test"]

这是我对整个数组的代码。我正在努力实现答案:

import UIKit

class ChecklistViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{

var dataHolder = [ListItem]()

var newChecklistItemString: String?
var alertInputTextField: UITextField?

@IBOutlet weak var myTableView: UITableView!

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var checkedItems: [ListItem] {
return dataHolder.filter { return $0.isChecked }
}
var uncheckedItems: [ListItem] {
return dataHolder.filter { return !$0.isChecked }
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return (dataHolder.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
cell.textLabel?.text = dataHolder[indexPath.row].title

return cell
}

// checkmarks when tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if (tableView.cellForRow(at: indexPath)?.accessoryType != .checkmark) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}else {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}

tableView.deselectRow(at: indexPath, animated: true)
saveDefaults()
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
checkedItems[indexPath.row].isChecked = false
myTableView.reloadData()
}
}


override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}

override func viewDidLoad() {
super.viewDidLoad()

addSlideMenuButton()

loadDefaults()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

@IBAction func addNewObject(_ sender: Any) {

let alert = UIAlertController(title: "New Item", message: nil, preferredStyle: .alert)
alert.addTextField { (alertInputTextField) in
alertInputTextField.autocapitalizationType = .sentences
}

alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: "Add", style: .default, handler: { (action) in

let textf = alert.textFields![0] as UITextField

let indexPath = IndexPath(row: self.dataHolder.count, section: 0)
self.dataHolder.append(ListItem(title: textf.text!, isChecked: false))
self.saveDefaults()
self.myTableView.insertRows(at: [indexPath], with: .automatic)

}))

self.present(alert, animated: true, completion: nil)

}

func loadDefaults()
{
self.dataHolder = UserDefaults.standard.array(forKey: "dataHolder") as? [ListItem] ?? []

}

func saveDefaults()
{
UserDefaults.standard.set(self.dataHolder, forKey: "dataHolder")

}
}

class ListItem {

var title: String
var isChecked: Bool

init(title: String, isChecked: Bool) {
self.title = title
self.isChecked = isChecked
}
}

最佳答案

你的代码太复杂了。当您使用类作为数据源时,额外的数组是多余的。

  • 删除 checkedItemsuncheckedItems

    var checkedItems: [ListItem] {
    返回 dataHolder.filter { 返回 $0.isChecked }
    }
    var uncheckedItems: [ListItem] {
    返回 dataHolder.filter { return !$0.isChecked }
    }

  • cellForRow中根据isChecked设置复选标记并重用单元格!

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0) // better set this in Interface Builder
    let data = dataHolder[indexPath.row]
    cell.textLabel?.text = data.title
    cell.accessoryType = data.isChecked ? .checkmark : .none
    return cell
    }
  • didSelectRowAt 中切换模型中的 isChecked更新特定行

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    dataHolder[indexPath.row].isChecked.toggle()
    tableView.reloadRows(at: [indexPath], with: .none)
    tableView.deselectRow(at: indexPath, animated: true)
    saveDefaults()
    }
  • tableView:commit:forRowAt: 中删除给定 indexPath 处的行

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
    dataHolder.remove(at: indexPath.row)
    myTableView.deleteRows(at: [indexPath], with: .fade)
    saveDefaults()
    }
    }
  • 并且您不能将自定义类的数组保存到 UserDefaults。我建议使用结构和 Codable

    struct ListItem : Codable {
    var title: String
    var isChecked: Bool
    }

    func loadDefaults()
    {
    guard let data = UserDefaults.standard.data(forKey: "dataHolder") else {
    self.dataHolder = []
    return
    }
    do {
    self.dataHolder = try JSONDecoder().decode([ListItem].self, for: data)
    } catch {
    print(error)
    self.dataHolder = []
    }
    }

    func saveDefaults()
    {
    do {
    let data = try JSONEncoder().encode(self.dataHolder)
    UserDefaults.standard.set(data, forKey: "dataHolder")
    } catch {
    print(error)
    }
    }

关于ios - 按单元格文本字符串值删除特定数组元素[swift 4],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559233/

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