gpt4 book ai didi

ios - 将对象从一个场景传递到下一个场景

转载 作者:行者123 更新时间:2023-11-28 13:13:24 24 4
gpt4 key购买 nike

我试图在 Swift 中将一个对象从一个场景传递到另一个场景,但我收到以下代码的错误:

Cannot invoke indexPathForSelectedRow with no arguments.

这是新要求吗?

在我看来,下面的代码应该可以工作,但我很困惑为什么不能。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!

var propertyArray: [Property] = [Property]()


override func viewDidLoad() {
super.viewDidLoad()

self.setUpProperties()

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func setUpProperties() {

var property1 = Property(cardImage: "image1.png", name: "Name1", location: "Hollywood")
var property2 = Property(cardImage: "image2.png", name: "Name2", location: "Astoria")
var property3 = Property(cardImage: "image3.png", name: "Name3", location: "Ft. Greene")

propertyArray.append(property1)
propertyArray.append(property2)
propertyArray.append(property3)

}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("FeedCell") as! CustomCell

let property = propertyArray[indexPath.row]

println(property.name + " " + property.location)

cell.setProperty(property.cardImage, name: property.name, location: property.location)

return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {

var detailPage = segue.destinationViewController as! DetailViewController

if let indexPath = self.tableView.indexPathForSelectedRow() {

let selectedProperty = propertyArray[indexPath.row]
detailPage.currentProperty = selectedProperty

}

}


}

最佳答案

问题在于这一行:

if let indexPath = self.tableView.indexPathForSelectedRow() {

您没有正确使用 View Controller 属性的名称。它的名称是 myTableView。所以把它改成这样:

if let indexPath = self.myTableView.indexPathForSelectedRow() {

问题解决了!

关于ios - 将对象从一个场景传递到下一个场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307355/

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