2016-03-21 10 views
0

Ich bekomme diesen Fehler in der Vorbereitung für die Funktion "segue", wenn ich meinem View Controller einen Abschnitt hinzufügen möchte. Dies ist der Fehler: "Ambigious Reference to member ' Tableview‘"Segue-Fehler "Ambigious Referenz zu Mitglied 'tableView'" Swift

Hier ist mein Code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if segue.identifier == "showCountryDetails" { 
    if let indexPath = self.tableView.indexPathForSelectedRow() { 
    let destinationController = segue.destinationViewController as! 
    DetailViewController 
    destinationController.countryImage = self.photos[indexPath.row] 
    } } 
} 

TravelTableViewController.swift

import UIKit 

class TravelTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var countries = ["Qatar","Brazil","United Kindgom","KSA"] 
    var cities = ["Doha","Rio","London","Riyadh"] 
    var photos = ["Qatar.jpg","Rio.jpg","Lndn.jpg","KSA.png"] 

    var countryIsVisited = [Bool](count: 4, repeatedValue: false) 
    var countryIsNotVisited = [Bool](count: 4, repeatedValue: false) 




    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

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

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

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

     cell.CityLabel.text = cities[indexPath.row] 
     cell.CountryLabel.text = countries[indexPath.row] 
     cell.ImageLabel.image = UIImage(named: photos[indexPath.row]) 

     cell.ImageLabel.layer.cornerRadius = cell.ImageLabel.frame.size.width/2 
     cell.ImageLabel.clipsToBounds = true 

     if countryIsVisited[indexPath.row] { 
      cell.accessoryType = .Checkmark 
     } 

     else{ 
      cell.accessoryType = .None 
     } 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let optionMenu = UIAlertController(title: nil, message: "What do you want to do ?", preferredStyle: .ActionSheet) 

     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
     optionMenu.addAction(cancelAction) 

     self.presentViewController(optionMenu,animated: true, completion: nil) 


     let callActionHandler = { (action:UIAlertAction!) -> Void in 
      let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry the phone is not avilable at the mometn", preferredStyle: .Alert) 
      alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
      self.presentViewController(alertMessage, animated: true, completion: nil) 
     } 

     let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: 
       UIAlertActionStyle.Default, handler: callActionHandler) 
     optionMenu.addAction(callAction) 

     let isVisitedAction = UIAlertAction(title: "I've been here", style: .Default, handler: { 
       (action:UIAlertAction!) -> Void in 
       let cell = tableView.cellForRowAtIndexPath(indexPath) 

       let checkImage = UIImage(named: "Hearts-48.png") 
       let checkMark = UIImageView(image: checkImage) 
       cell?.accessoryView = checkMark 
       self.countryIsVisited[indexPath.row] = true 
       }) 
     optionMenu.addAction(isVisitedAction) 

     let isNotVisitedAction = UIAlertAction(title: "I haven't been here", style: .Default, handler: { 
      (action:UIAlertAction!) -> Void in 
      let cell = tableView.cellForRowAtIndexPath(indexPath) 
      cell?.accessoryView = .None 
      self.countryIsNotVisited[indexPath.row] = true 
     }) 
     optionMenu.addAction(isNotVisitedAction) 





    } 





    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
     let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: 
      "Share", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 


      let shareMenu = UIAlertController(title: nil, message: "Share using", 
       preferredStyle: .ActionSheet) 
      let twitterAction = UIAlertAction(title: "Twitter", style: 
       UIAlertActionStyle.Default, handler: nil) 
      let facebookAction = UIAlertAction(title: "Facebook", style: 
       UIAlertActionStyle.Default, handler: nil) 
      let emailAction = UIAlertAction(title: "Email", style: UIAlertActionStyle.Default, 
       handler: nil) 
      let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, 
       handler: nil) 
      shareMenu.addAction(twitterAction) 
      shareMenu.addAction(facebookAction) 
      shareMenu.addAction(emailAction) 
      shareMenu.addAction(cancelAction) 



      self.presentViewController(shareMenu, animated: true, completion: nil) 
      }) 
     let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, 
       title: "Delete",handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
       // Delete the row from the data source 
        self.countries.removeAtIndex(indexPath.row) 
        self.cities.removeAtIndex(indexPath.row) 
        self.photos.removeAtIndex(indexPath.row) 

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 



       }) 


     //Adding Background Color to the Delete and Share Button 
     shareAction.backgroundColor = UIColor(red: 255.0/255.0, green: 166.0/255.0, blue: 
        51.0/255.0, alpha: 1.0) 
     deleteAction.backgroundColor = UIColor(red: 51.0/255.0, green: 51.0/255.0, blue: 
        51.0/255.0, alpha: 1.0) 


     return [deleteAction, shareAction] 


    } 


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
     if segue.identifier == "showRestaurantDetail" { 
     if let indexPath = self.tableView.indexPathForSelectedRow() { 
     let destinationController = segue.destinationViewController as! 
     DetailViewController 
     destinationController.countryImage = self.photos[indexPath.row] 
     } } 
    } 

    } 
+0

Haben Sie die Steckdose von 'UITableView' genommen? –

Antwort

1

machen, wo die Verbindungs ​​Outlet zu Ihrem tableView ist? Scheint, dass Ihr Code es nicht hat. Control-Drag von Storyboard (wenn Sie sie verwenden) und Ihr Fehler sollte weggehen.

1

indexPathForSelectedRow ist eine Eigenschaft, keine Methode, entfernen Sie die Klammern

if let indexPath = self.tableView.indexPathForSelectedRow { 
+0

Seem der Fehler ging nicht weg, selbst nach dem Entfernen der Klammern – AHmed

1

Nur UIViewController-UITableViewController in Ihrer Klassendefinition ändern.

Dies wird die self.tableView verfügbar