Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

ios - Conforming to UITableViewDelegate and UITableViewDatasource in Swift?

I am learning swift 2.0, and I was wondering if you still need to add the code tableView.datasource = self and tableView.delegate = self like in Obj-C to conform to the protocols?

Example code:

class AboutViewController: UIViewController, UITableViewDataSource,   UITableViewDelegate
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // conform to protocols
        aboutTableView.dataSource = self
        aboutTableView.delegate = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 2
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return 50
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        // Code here
    }
}

Now the table view loads with the correct data in each cell.

However, if I remove aboutTableView.datasource = self and aboutTableView.delegate = self from viewDidLoad, my table view is blank. Why is this?

Is this code still required because I see many youtube tutorials that does not include this anymore in swift, and I'm confused as to why mine doesn't work without it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, that is completely independent of which language you use, Swift or Objective-C.

But there are two different cases which may cause the confusion:

A UITableViewController subclass:

UITableViewController already conforms to UITableViewDataSource and UITableViewDelegate. It has a tableView property, whose dataSource and delegate property are already set to self.

In your subclass, you typically override the data source and delegate methods.

A UIViewController subclass with a UITableView property:

Here you have defined a UITableView property in your subclass and initialize it in your code, or connect it to a table view in the interface builder.

In this case you have to set the dataSource and delegate property of the tableview, either in code or in the interface builder, as explained in luk2302's answer.

If data source and delegate are the view controller itself, then you have to declare the protocol conformance explicitly, and implement the data source and delegate methods (but without overriding a superclass method).


Of course, in both cases, the table view data source and the delegate can be set to a different object, it does not have to be the view controller itself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...