gpt4 book ai didi

swift - 选定的行将 `nil` 传递到详细 View

转载 作者:行者123 更新时间:2023-11-30 10:04:40 25 4
gpt4 key购买 nike

我有一个带有Table View的屏幕,它显示了存储在对象currentReminders: NSMutableArray中的所有提醒。

现在我正在点击一排提醒列表,进入一个屏幕以显示其详细信息。

我想要做的是:选择一行,获取行索引,然后在该行索引处选择数组中相应的提醒对象以传递到下一个 View 。

我该怎么做?我是 swift 的新手。这是我的练习任务。

最佳答案

您走在正确的道路上。当您点击表中触发 segue 的单元格时,prepareForSegue 的发送者参数就是该单元格。您可以使用 tableView 导出和单元格对象来获取 indexPath 并使用它从数组中检索数据。

class ReminderListController: UITableViewController, addReminderProtocol {

// assuming that the following gets loaded somehow.
var currentReminders = [Reminders]()
var reminderToPass: Reminders?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showReminderSegue" {
// convert sender to a cell
if let cell = sender as? UITableViewCell {
// get the indexPath of that cell in the tableView
let indexPath = tableView.indexPathForCell(cell)
if let destinationVC = segue.destinationViewController as? ShowReminderController {
destinationVC.aReminder = currentReminders[indexPath.row]
}
}
}
}
}

关于swift - 选定的行将 `nil` 传递到详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36753441/

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