2015-11-01 55 views
-1

我創建了這個程序,但我有這個持續存在的問題,我的ViewController不符合UITableViewDataSource。我不知道還有什麼要做。我在論壇中無處不在。 你能發現任何問題嗎?類型ViewController不符合協議UITableViewData來源

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIPopoverPresentationControllerDelegate,UITableViewDataSource, UITableViewDelegate { 
    struct MyData { 
     var imagy:UIImage 
     var title:String 
     var details:String 
    } 

    var tableData: [MyData] = [] 

    @IBOutlet weak var mapView: MKMapView! 

    let locationManager = CLLocationManager() 

    var mapItemData:MKMapItem! 

    let textCellIdentifier = "TextCell" 

    override func prepareForSegue(segue: UIStoryboardSegue, 
     sender: AnyObject?){ 
      // Set any data to be shown on the table view here 
    } 

    var picture:[UIImage] = [ 
     UIImage(named: "pic1.jpg")!, 
     UIImage(named: "pic2.jpg")!, 
     UIImage(named: "pic3.jpg")!, 
     UIImage(named: "pic4.jpg")!, 
     UIImage(named: "pic5.jpg")!, 
     UIImage(named: "pic6.jpg")!, 
     UIImage(named: "pic7.jpg")!, 
     UIImage(named: "pic8.jpg")!, ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // var imageView : UIImageView 
     // imageView = UIImageView(frame:CGRectMake(10, 50, 100, 300)); 
     // imageView.image = UIImage(named:"image.jpg") 
     // self.view.addSubview(imageView) 

     tableData = [ 
      MyData(imagy:UIImage(named: "pic1.jpg")! ,title: "The first row", details: "Hello"), 
     ] 

     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestWhenInUseAuthorization() 
     self.locationManager.startUpdatingLocation() 

     mapView.delegate = self 

     let J = Shops(title: "Jingle", coordinate: CLLocationCoordinate2D(latitude: 44.631076, longitude: 22.946770), info: "H", address:"Π", subtitle:"κ") 
     let K = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 43.65, longitude: 6.43), info: "F", address:"Π", subtitle:"κ") 
     let L = Shops(title: "P", coordinate: CLLocationCoordinate2D(latitude: 58.89, longitude: 2.3508), info: "O", address:"Π", subtitle:"κ") 
     let B = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 26, longitude: 17), info: "H", address:"Π", subtitle:"κ") 
     let W = Shops(title: "W", coordinate: CLLocationCoordinate2D(latitude: 88.43111, longitude: -37.035663), info: "N", address:"Π", subtitle:"κ") 

     mapView.addAnnotations([J, K, L, B, W]) 
    } 

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

      func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
       if let annotation = annotation as? Shops{ 
        let identifier = "pin" 
        var view: MKPinAnnotationView 
        if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
         as? MKPinAnnotationView { 
          dequeuedView.annotation = annotation 
          view = dequeuedView 
        } else { 

         view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
         view.canShowCallout = true 
         view.calloutOffset = CGPoint(x: -5, y: 5) 

         view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView 
        } 
        return view 
       } 
       return nil 
      } 

    // Initiate GPS 
    func fitMapViewToAnnotaionList(annotations: [MKPointAnnotation]) -> Void { 
     let mapEdgePadding = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) 
     var zoomRect:MKMapRect = MKMapRectNull 

     for index in 0..<annotations.count { 
      let annotation = annotations[index] 
      let aPoint:MKMapPoint = MKMapPointForCoordinate(annotation.coordinate) 
      let rect:MKMapRect = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1) 

      if MKMapRectIsNull(zoomRect) { 
       zoomRect = rect 
      } else { 
       zoomRect = MKMapRectUnion(zoomRect, rect) 
      } 

      // Locate through GPS 

      func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
       let location = locations.last 
       let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
       let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) 

       self.mapView.setRegion(region, animated: true) 
       self.locationManager.stopUpdatingLocation() 
      } 
     } 

     //Pressing Button and segue 
      func Button(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl){ 
       self.performSegueWithIdentifier("TableViewCell", sender: self) 
      } 

     //TableViewDataSource functions 
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return tableData.count 
     } 

     func numberOfSectionsInTableView(tableView: UITableView) -> Int{} 

     func table(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      // Create a new cell with the reuse identifier of our prototype cell 
      // as our custom table cell class 
      let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewController 
      // Set the first row text label to the firstRowLabel data in our current array item 
      cell.imagy.image = tableData[indexPath.row].imagy 
      // Set the second row text label to the secondRowLabel data in our current array item 
      cell.title.text = tableData[indexPath.row].title 
      // Set the second row text label to the secondRowLabel data in our current array item 
      cell.details.text = tableData[indexPath.row].details 
      // Return our new cell for display 
      return cell 

      tableView.delegate = self 
      tableView.dataSource = self 
     } 
    } 
} 
+1

'numberOfSectionsInTableView()'不返回任何東西。 –

+0

我知道,我只是把它放在那裏,因爲我在Stack中的另一個主題中閱讀它,那裏的人有同樣的問題 –

回答

3

當心{}和縮進:您的tableView數據源方法都嵌套在fitMapViewToAnnotaionList方法中。將它們從該方法中移出到課程本身中。

另外,cellForRowAtIndexPath方法作爲

func table(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

實現這應該是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

正如其他人所說,numberOfSectionsInTableView應該返回一個值;您可能需要1(或完全刪除該方法;協議不要求)。

所以這應該擺脫的問題與符合協議(雖然有可能是其它無關的問題):

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIPopoverPresentationControllerDelegate,UITableViewDataSource, UITableViewDelegate { 
    struct MyData { 
     var imagy:UIImage 
     var title:String 
     var details:String 
    } 

    //TableViewDataSource functions 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tableData.count 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{ return 1 } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Create a new cell with the reuse identifier of our prototype cell 
     // as our custom table cell class 
     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewController 
     // Set the first row text label to the firstRowLabel data in our current array item 
     cell.imagy.image = tableData[indexPath.row].imagy 
     // Set the second row text label to the secondRowLabel data in our current array item 
     cell.title.text = tableData[indexPath.row].title 
     // Set the second row text label to the secondRowLabel data in our current array item 
     cell.details.text = tableData[indexPath.row].details 
     // Return our new cell for display 
     return cell 

     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    var tableData: [MyData] = [] 

    @IBOutlet weak var mapView: MKMapView! 

    let locationManager = CLLocationManager() 

    var mapItemData:MKMapItem! 

    let textCellIdentifier = "TextCell" 

    override func prepareForSegue(segue: UIStoryboardSegue, 
     sender: AnyObject?){ 
      // Set any data to be shown on the table view here 
    } 

    var picture:[UIImage] = [ 
     UIImage(named: "pic1.jpg")!, 
     UIImage(named: "pic2.jpg")!, 
     UIImage(named: "pic3.jpg")!, 
     UIImage(named: "pic4.jpg")!, 
     UIImage(named: "pic5.jpg")!, 
     UIImage(named: "pic6.jpg")!, 
     UIImage(named: "pic7.jpg")!, 
     UIImage(named: "pic8.jpg")!, ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // var imageView : UIImageView 
     // imageView = UIImageView(frame:CGRectMake(10, 50, 100, 300)); 
     // imageView.image = UIImage(named:"image.jpg") 
     // self.view.addSubview(imageView) 

     tableData = [ 
      MyData(imagy:UIImage(named: "pic1.jpg")! ,title: "The first row", details: "Hello"), 
     ] 

     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestWhenInUseAuthorization() 
     self.locationManager.startUpdatingLocation() 

     mapView.delegate = self 

     let J = Shops(title: "Jingle", coordinate: CLLocationCoordinate2D(latitude: 44.631076, longitude: 22.946770), info: "H", address:"Π", subtitle:"κ") 
     let K = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 43.65, longitude: 6.43), info: "F", address:"Π", subtitle:"κ") 
     let L = Shops(title: "P", coordinate: CLLocationCoordinate2D(latitude: 58.89, longitude: 2.3508), info: "O", address:"Π", subtitle:"κ") 
     let B = Shops(title: "R", coordinate: CLLocationCoordinate2D(latitude: 26, longitude: 17), info: "H", address:"Π", subtitle:"κ") 
     let W = Shops(title: "W", coordinate: CLLocationCoordinate2D(latitude: 88.43111, longitude: -37.035663), info: "N", address:"Π", subtitle:"κ") 

     mapView.addAnnotations([J, K, L, B, W]) 
    } 

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

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     if let annotation = annotation as? Shops{ 
      let identifier = "pin" 
      var view: MKPinAnnotationView 
      if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
       as? MKPinAnnotationView { 
        dequeuedView.annotation = annotation 
        view = dequeuedView 
      } else { 

       view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       view.canShowCallout = true 
       view.calloutOffset = CGPoint(x: -5, y: 5) 

       view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView 
      } 
      return view 
     } 
     return nil 
    } 

    // Initiate GPS 
    func fitMapViewToAnnotaionList(annotations: [MKPointAnnotation]) -> Void { 
     let mapEdgePadding = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) 
     var zoomRect:MKMapRect = MKMapRectNull 

     for index in 0..<annotations.count { 
      let annotation = annotations[index] 
      let aPoint:MKMapPoint = MKMapPointForCoordinate(annotation.coordinate) 
      let rect:MKMapRect = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1) 

      if MKMapRectIsNull(zoomRect) { 
       zoomRect = rect 
      } else { 
       zoomRect = MKMapRectUnion(zoomRect, rect) 
      } 

      // Locate through GPS 

      func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
       let location = locations.last 
       let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
       let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) 

       self.mapView.setRegion(region, animated: true) 
       self.locationManager.stopUpdatingLocation() 
      } 
     } 

     //Pressing Button and segue 
     func Button(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl){ 
      self.performSegueWithIdentifier("TableViewCell", sender: self) 
     } 
    } 
} 
+0

你好,謝謝你的回答。我改變了{},甚至在不同的地方把整個東西都搬了上去,但問題依然存在。 我也將函數改爲「tableView」 –

+0

爲了確保它在類中,但是它仍然不起作用,我把「Table View Functions」的整個包放在「Struct MyData」下面第一件事 –

+0

@ChrisVlachos I已經添加了修改代碼,可以爲我編譯OK。 – pbasdf

相關問題