The UITableView in swift language is really very pit daddy, why, because after following the agreement often reported such error: does not conform to protocol ' Uitableviewdatasource '. And is the first time to try the partners will often find that I write the code is not a problem ah, the write is written, why still error, and sometimes is the problem of Xcode, and sometimes it is necessary to follow the Protocol has to implement the method and did not lead to the implementation. So in this case, you might as well jump into the agent to see that the methods that must be implemented are not.
Here is a small demo I wrote, we can see.
Import UIKit
Class Viewcontroller:uiviewcontroller, Uitableviewdatasource, uitableviewdelegate{
var dataarray:nsmutablearray?
var tableview:uitableview?
Override Func Viewdidload () {
Super.viewdidload ()
Additional setup after loading the view, typically from a nib.
Self.view.backgroundColor = Uicolor.orangecolor ()
Self.navigationItem.title = "Hello Swift"
Self.dataarray = Nsmutablearray ()
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
self.dataarray!. AddObject ("Hello Swift")
NSLog ("%@", self.dataarray!)
Self.tableview = UITableView (Frame:self.view.frame, Style:UITableViewStyle.Plain)
self.tableview!. delegate = Self
self.tableview!. DataSource = Self
self.tableview!. RegisterClass (uitableviewcell.self, Forcellreuseidentifier: "Mytestcell")
Self.view.addSubview (self.tableview!)
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any resources the can be recreated.
}
Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), int{
Return self.dataarray!. Count
}
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), uitableviewcell{
Let Cell:uitableviewcell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "Mytestcell")
var string:string = Self.dataarray?. Objectatindex (Indexpath.row) as String
Cell.textLabel.text = string
Cell.textLabel.text = string
return cell
}
Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) {
Tableview.deselectrowatindexpath (Indexpath, Animated:true)
var controller = Secondviewcontroller ()
Controller.title = "Swift's self-introduction"
Controller.view.backgroundColor = Uicolor (red:10/255.0, green:100/255.0, blue:200/255.0, alpha:1.0)
Self.navigationcontroller?. Pushviewcontroller (Controller, Animated:true)
}
}
Swift implements UITableView error, does not conform to protocol ' Uitableviewdatasource '