Swift 如何实现从uitableviewcell到另一个tableviewcell的转换
我有两个带有tableviewcell的视图控制器,我可以在这两个控制器中添加项目 我想要实现的是,当我在第一个tableviewcell中添加项目时,让我们假设我添加(项目1,项目2)并按下(项目1) 我想转到第二个tableviewcell并添加数据 但我希望第二个tableviewcell中的数据单独保存 这意味着,现在如果我按下(第一项),我应该会看到我添加的数据 但如果我按下(第二项),它应该是空的,我可以稍后添加数据Swift 如何实现从uitableviewcell到另一个tableviewcell的转换,swift,uitableview,Swift,Uitableview,我有两个带有tableviewcell的视图控制器,我可以在这两个控制器中添加项目 我想要实现的是,当我在第一个tableviewcell中添加项目时,让我们假设我添加(项目1,项目2)并按下(项目1) 我想转到第二个tableviewcell并添加数据 但我希望第二个tableviewcell中的数据单独保存 这意味着,现在如果我按下(第一项),我应该会看到我添加的数据 但如果我按下(第二项),它应该是空的,我可以稍后添加数据 我已将两个tableviewcell的数据保存在coredata中
我已将两个tableviewcell的数据保存在coredata中。我在该解决方案中不使用segues或coredata,但该解决方案至少会在某种程度上解决您需要的问题
class vc1: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
let options: [Int] = [1,2]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return options.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = vc2()
//Not sure where to put the setup to be honest
//option 1:
vc.setupTableView(forCell: indexPath.item)
present(vc, animated: true) {
//option 2:
vc.setupTableView(forCell: indexPath.item)
}
}
}
class vc2: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
var selectedCell: Int!
//Having an nested array in an array will solve this custom "page" you are looking for
let results: [[String]] = [["test1", "test2"], ["test3", "test4"]]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func setupTableView(forCell cell: Int) {
selectedCell = cell
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return results[selectedCell].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel = results[selectedCell][indexPath.item]
return UITableViewCell()
}
}
首先显示您的代码