类型ViewController不符合协议UITableViewData Source

时间:2015-11-01 21:40:28

标签: ios uitableview uiviewcontroller

我创建了这个程序但是我有这个持久存在的问题,我的ViewController不符合UITableViewDataSource。我不知道还能做什么。我在论坛里到处寻找。 你能发现任何问题吗?

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 个答案:

答案 0 :(得分:3)

注意{}和缩进:tableView数据源方法嵌套在cellForRowAtIndexPath方法中。将它们从该方法中移出到类本身中。

此外,func table(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 方法实现为

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

应该是:

numberOfSectionsInTableView

正如其他人所说,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) } } } 应该返回一个值;你可能想要1(或完全删除方法;协议不要求它。)

所以这应该摆脱符合协议的问题(尽管可能存在其他无关的问题):

mathematician1975