【问题标题】:move a table view cell from one table view to a new table view将表格视图单元格从一个表格视图移动到新的表格视图
【发布时间】:2020-05-17 01:22:31
【问题描述】:

目前,我可以将表格视图单元格从一个表格视图移动到另一个表格视图,但在新的表格视图中,我想将此单元格从“标记为已完成”部分移动到“历史记录”部分。现在程序正在运行,但随后在“goals[1].append(goals[0][indexPath.row])”行崩溃,并出现“致命错误:索引超出范围”。我该如何解决这个问题?

更新了进度视图的代码。

let sections: [String] = [“Mark as Complete”, “History”]
var goals: [[String]] = []


extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if goals.indices.contains(section) {
        return goals[section].count
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TodayGoalViewCell_1", for: indexPath) as? GoalTableViewCell
        cell?.goalLabel.text = goals[indexPath.section][indexPath.row]
        cell?.cellDelegate = self
        cell?.index = indexPath
        return cell!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section]
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
    goals[0].remove(at: indexPath.row)
    goals[1].append(goals[0][indexPath.row])
    tableView.reloadData()
    }

}

在单独的文件中查看常规目标。代码如下:

var goals: [String] = ["goal 1", "goal 2", "goal 3"]
valueToPass = “ ”

override func viewDidLoad() {
    super.viewDidLoad() 
    tableView.delegate = self
    tableView.dataSource = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier = “GoalCell_1” {
    let viewController = segue.destination as! ViewController
    viewController.goals.append([valueToPass])
    }
}

extension GoalsViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return goals.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GoalCell_1", for: indexPath)
        cell.textLabel?.text = goals[indexPath.row]
        cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
        cell.textLabel?.numberOfLines = 3
        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        valueToPass = goals[indexPath.row]
        PerformSegue(withIdentifier: “segue”, sender: self)
        if indexPath.section == 0 {
            goals.remove(at: indexPath.row)
            goals.count != 0 {
                showGoalSelected()
                tableView.reloadData()
        }
    }
}

【问题讨论】:

  • 如何在两个视图之间移动?

标签: ios swift xcode uitableview


【解决方案1】:

因此,通过使用 segue 在 2 行之间移动,您可以使用 didSelectRowAtIndexPath() 函数来选择目标并通过 segue 将其传递给进度视图。 为此,您需要在进度视图中排列以接受您拥有的目标。

var goals: [String] = []

您还需要设置一个 segue,最简单的方法是按住 ctrl 单击并从情节提要顶部的 viewController 图标拖动目标视图,然后将其拖放到 progressGoals 视图上。您会在下拉菜单中选择转场类型,选择 1。然后使用右侧菜单将转场命名为令人难忘的名称。

你还需要一个地方来保持球门传球。将此添加到您的目标视图中。

var valueToPass = ""

现在实现类似这样的选择行功能。

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        
        print("You selected cell #\(indexPath.row)!")
        //selects the goal from the goals array
        valueToPass = goals[indexPath.row]

        //trigger the segue to move views
        performSegue(withIdentifier: <yourseguename>, sender: self)
       
    }

现在在 segue 中将值传递给进度目标视图。

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if (segue.identifier == "yourseguename"){
            
            //this is the view controller of the where progress goals table is
            let viewController = segue.destination as! ViewController 
            //add the goal you selected to the array in you progress goals view. 
            viewController.goals.append(valueToPass)
        }
    }

希望有帮助

【讨论】:

  • @Kar534 你有什么错误吗?信息是否在进度视图中传递给数组?
  • 我看到了您的另一个问题,我认为您有 2 个变量,称为目标 1,大写 G,另一个小写 g,因此试图在第二个视图中访问错误的变量。
  • 好的,这可能与您在进度表视图中的目标数组有关运行打印语句以查看其中存储的内容。也许首先将它作为一个简单的数组进行测试,然后将数组数组的复杂性添加到节中。
  • 回顾您的其他代码,准备转场功能中的转场标识符似乎存在一些命名错误,这与选择单元格触发的错误不同。您能否再次发布您现在拥有的代码?
  • 好的,那么你在哪里触发 segue? segue叫什么?目前您有一个与单元标识符相同的 segue 标识符?
猜你喜欢
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多