Swift implements UITableView error, does not conform to protocol ' Uitableviewdatasource '

Source: Internet
Author: User

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 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.