我对Xcode和Swift相对较新,我正在遵循这个指南:https://codewithchris.com/iphone-app-connect-to-mysql-database/#app但是有三个变量即使在之前被正确识别也没有被识别出来:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
// Get the location to be shown
let item: InputModel = feedItems[indexPath.row] as! InputModel
// Get references to labels of cell
myCell.textLabel!.text = item.Street
return myCell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set selected location to var
selectedLocation = feedItems[indexPath.row] as! InputModel
// Manually call segue to detail view controller
self.performSegue(withIdentifier: "detailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get reference to the destination view controller
let detailVC = segue.destination as! DetailViewController
// Set the property to the selected location so when the view for
// detail view controller loads, it can access that property to get the feeditem obj
detailVC.selectedLocation = selectedLocation
}
我得到的结果是“使用未解析的标识符selectedLocation”两次和“使用未解析的标识符feedItems”。我还应该说我将locationModel()的标题更改为InputModel,并将地址更改为street。如果需要,我可以解释更多并添加源文件。
编辑:抱歉,变量是先前设置的,在这里:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocol {
//Properties
var feedItems: NSArray = NSArray()
var selectedLocation : InputModel = InputModel()
@IBOutlet weak var listTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//set delegates and initialize homeModel
self.listTableView.delegate = self
self.listTableView.dataSource = self
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
}
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
答案 0 :(得分:1)
我认为罪魁祸首是额外的结束括号“}”。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
// Get the location to be shown
let item: InputModel = feedItems[indexPath.row] as! InputModel
// Get references to labels of cell
myCell.textLabel!.text = item.Street
return myCell
}
} <================ Delete this bracket because it is redundant here and move to the end ======
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
....
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
} <======== MOVE IT HERE
答案 1 :(得分:0)
基本上你错过了两个变量“selectedLocation”和“feedItems”的声明
从您发布的代码中可以看出这一行
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Missing the declaration of both variables here
selectedLocation = feedItems[indexPath.row] as! InputModel
// The rest of the code
}
这里只缺少一个变量
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// previous code ...
// Missing variable declaration in the next line
let item: InputModel = feedItems[indexPath.row] as! InputModel
// rest of the code
}
}
不知道声明的位置和方式,很难知道错误的位置。
首先,这些变量在同一个类中是否属于这些函数?
如果不是那个问题,你需要获取他们设置的类的实例,例如
otherclass.selectedLocation = otherclass.feedItems[indexPath.row] as! InputModel
如果是,请尝试使用自我声明访问它们(它可能没有必要,但它可能有用),例如
self.selectedLocation = self.feedItems[indexPath.row] as! InputModel
如果你能提供设置这些变量的代码
会更容易