Swift 如何解决错误:引用成员';indexPathForRow';如果没有上下文类型,则无法解析
我尝试用tableview实现一个Longpress函数,它应该给我选择的行。 只是我遇到了一个我无法解决的错误Swift 如何解决错误:引用成员';indexPathForRow';如果没有上下文类型,则无法解析,swift,objective-c,Swift,Objective C,我尝试用tableview实现一个Longpress函数,它应该给我选择的行。 只是我遇到了一个我无法解决的错误 override func viewDidLoad() { super.viewDidLoad() let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(FlightsViewController.longPress(longPressGestureRe
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(FlightsViewController.longPress(longPressGestureRecognizer:)))
self.view.addGestureRecognizer(longPressRecognizer)
}
@objc func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print(indexPath)
}
}
}
这给了我一个错误:没有上下文类型,无法解析对成员“indexPathForRow”的引用
请帮忙。先谢谢你
这是表视图的声明
extension FlightsViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return prefixArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FlightsTableViewCell", for: indexPath) as! FlightsTableViewCell
cell.lbDate.text = self.dateArray[indexPath.row]
if UserDefaults.standard.string(forKey: "update") == "OK" {
cell.lbMiles.text = String(self.milesArray[indexPath.row])
if self.saciArray[indexPath.row] == true {
cell.lbPrefix.textColor = UIColor.green
} else {
cell.lbPrefix.textColor = UIColor.white
}
}
cell.lbPrefix.text = self.prefixArray[indexPath.row]
cell.lbStart.text = self.startArray[indexPath.row]
cell.lbStop.text = self.stopArray[indexPath.row]
cell.lbTakeOff.text = self.takeOffArray[indexPath.row]
cell.lbLanding.text = self.landingArray[indexPath.row]
cell.lbFlighTime.text = "\(round(10 * Double(self.flightTimeArray[indexPath.row])!) / 10)"
cell.lbEngineTime.text = "\(round(10 * Double(self.engineTimeArray[indexPath.row])!) / 10)"
cell.lbOrigin.text = self.originArray[indexPath.row]
cell.lbDestination.text = self.destinationArray[indexPath.row]
cell.lbInformation.text = self.informationArray[indexPath.row]
cell.lbPousos.text = self.pousosArray[indexPath.row]
if self.rulesArray[indexPath.row] == 0 {
cell.lbRules.text = "VFR" } else { cell.lbRules.text = "IFR"
}
if self.dayNightArray[indexPath.row] == 0 {
cell.lbDayNight.text = messageDay } else { cell.lbDayNight.text = messageNight
}
if self.pilotCopilotArray[indexPath.row] == 0 {
cell.lbPilotCopilot.text = messagePilot } else { cell.lbPilotCopilot.text = messageCoPilot
}
if self.engineArray[indexPath.row] == 0 {
cell.lbEngine.text = messageSingleEngine } else { cell.lbEngine.text = messageTwinEngine
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.idEditFlight = idArray[indexPath.row]
performSegue(withIdentifier: "editFlight", sender: self)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == (idArray.count - 1) {
readFlights()
}
}
tableview如何声明或在何处声明?扩展FlightsViewController:UITableViewDelegate,UITableViewDataSource请查看为表视图声明添加的代码。谢谢你,不是这样,你在问题中随意添加了一个扩展名。在视图控制器的某个地方,您已经声明了
var tableView:Something
。错误是“我不知道tableView是什么类型的东西,告诉我它是什么”。在您告诉编译器该信息之前,您的错误将保持“您的意思是:@IBOutlet弱var tabelView:UITableView!