gpt4 book ai didi

ios - 更新 : How to checkmark select items from selection List by clicking Tableview cell in swift

转载 作者:行者123 更新时间:2023-12-03 17:56:07 25 4
gpt4 key购买 nike

我正在尝试从 ViewController 检索 tableView 中的选定项目它有一个来自 sqlite 的列表,如复选标记选择。我的 FirstViewController有 tableView 并在单击选择性单元格时打开 ViewController选择项目。
一旦做出选择,我必须重新加载 FirstViewController TableView 。实际上我正在尝试做但没有准确地做。任何人都请帮我选择项目并在表格 View 中设置。我很困惑,无法通过 segue 进行传递选择。
我的github项目链接如下:

我的项目 : https://github.com/MasamMahmood/SqliteDataList/tree/master/SqliteDataList

引用:https://blog.apoorvmote.com/how-to-pass-selection-via-segue/

更新了一个 FirstViewController:

class FirstViewController: UIViewControlller, ListDelegate {

var selectedIndex: Int?
var selectedSection: Int?

//Click event for navigation from FirstViewController to SecondViewController
@IBAction func BackButtonAction(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}

func listFunc(listValue: String) {
AppData?.sectionList?[selectedSection!].items?[selectedIndex!].textField = listValue
tableView.reloadData()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Add these two line in your code
selectedIndex = indexPath.row
selectedSection = indexPath.section
}
}

更新了 SecondViewController:
protocol ListDelegate {
func listFunc(listValue: String)
}

class SecondViewController: UIViewControlller {
var delegate: ListDelegate?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
//print(currentCell.textLabel?.text as Any)
currentCell.accessoryType = .checkmark
delegate?.listFunc(listValue: currentCell.textLabel?.text ?? "")
self.navigationController?.popViewController(animated: true)
}
}

最佳答案

您需要一个委托(delegate)来与 FirstViewController 进行通信。你可以这样做...

protocol ViewControllerDelegate: AnyObject {
func viewControllerDidMakeSelection(at index: Int)
}

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

weak var delegate: ViewControllerDelegate?

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.viewControllerDidMakeSelection(at: indexPath.row)
}
}

class FirstViewController: UIViewController, ViewControllerDelegate {

@IBOutlet weak var tableView: UITableView!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}

func viewControllerDidMakeSelection(at index: Int) {
// This is where the magic happens
}
}

关于ios - 更新 : How to checkmark select items from selection List by clicking Tableview cell in swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59128031/

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