Ich erstelle eine App, die Daten von Firebase abruft. In meinem 'MealViewController' habe ich eine TableView, die den View-Controller als Delegate und Datenquelle hat. Ich bekomme das Problem "Typ" MealViewController "entspricht nicht dem Protokoll 'UITableViewDataSource', weil es beide benötigt: numberOfRowsInSection: und: cellForRowAtIndexPath:. Wenn ich jedoch beide hinzufüge, erscheint ein weiteres Problem - "Definitionskonflikt mit vorherigem Wert". Ich habe alle Probleme mit dem Stack-Überlauf durchgesehen und es gab kein Glück. Hier ist mein View Controller:cellForRowAtIndexPath und numberOfRowsInSection in tableView widersprechen
class MealViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var bgImage: UIImageView?
var image : UIImage = UIImage(named: "pizza")!
@IBOutlet weak var blurEffect: UIVisualEffectView!
@IBOutlet weak var mealTableView: UITableView!
var items = [MealItem]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bgImage = UIImageView(image: image)
bgImage?.contentMode = .ScaleAspectFill
bgImage!.frame = view.layer.bounds
self.view.addSubview(bgImage!)
//self.bgImage?.addSubview(blurEffect)
//bgImage!.bringSubviewToFront(blurEffect)
view.bringSubviewToFront(blurEffect)
mealTableView.layer.cornerRadius = 5.0
mealTableView.layer.borderColor = UIColor.whiteColor().CGColor
mealTableView.layer.borderWidth = 0.5
let ref = Firebase(url: "https://order-template.firebaseio.com/grocery-items")
mealTableView.delegate = self
mealTableView.dataSource = self
// MARK: UIViewController Lifecycle
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(items.count)
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> MealsCellTableViewCell { //issue occurs here
let groceryItem = items[indexPath.row]
if let cell = mealTableView.dequeueReusableCellWithIdentifier("ItemCell") as? MealsCellTableViewCell {
cell.configureCell(groceryItem)
// Determine whether the cell is checked
self.mealTableView.reloadData()
return cell
}
}
func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// [1] Call the queryOrderedByChild function to return a reference that queries by the "completed" property
ref.observeEventType(.Value, withBlock: { snapshot in
var newItems = [MealItem]()
for item in snapshot.children {
let mealItem = MealItem(snapshot: item as! FDataSnapshot)
newItems.append(mealItem)
}
self.items = newItems
self.mealTableView.reloadData()
})
}
func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
}
func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
}
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
bgImage = UIImageView(image: image)
bgImage?.contentMode = .ScaleAspectFill
bgImage!.frame = view.layer.bounds
self.view.addSubview(bgImage!)
view.bringSubviewToFront(blurEffect)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITableView Delegate methods
}
Bitte zeigen Sie an, in welcher Zeile dieser Fehler erscheint. Vielleicht hat es überhaupt nichts mit der Tabellenansicht zu tun. –
Das erste Problem ist in der Klasse-Anweisung, die zweite, die ich bearbeitet und markiert, die auf der CellForRowAtIndexPath ist –
Oh, bemerkte gerade Ihre DataSource-Methoden sind tatsächlich geschachtelt :) So ist Ihre 'CellForRowAtIndexPath' nur eine lokale Funktion, die unsichtbar ist außerhalb von 'viewDidLoad'. Sie sollten alle Ihre geschachtelten Funktionen aus 'viewDidLoad' verschieben. –