Conform to protocol in ViewController, in Swift

31,958

Solution 1

You use a comma:

class GameList: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // ...
}

But realize that the super class must be the first item in the comma separated list.

If you do not adopt all of the required methods of the protocol there will be a compiler error. You must get all of the required methods!

Solution 2

As XCode6-Beta7 releases,

I noticed the protocol method of UITableViewDataSource changed a little bit and sounded the same conform to protocol error which worked fine in beta6.

These are the required methods to be implemented according to the UITableViewDataSource protocol:

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // insert code
}

You might want to re-check the difference or re-implement the delegate method that you thought you just implement.

Solution 3

You must implement two require methods here:

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

func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

    cell.text = "Row #\(indexPath.row)"
    cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"

    return cell
}

Solution 4

Also, it is important to copy all the non optional functions from the Delegate class. Cmd + Click on the UITableViewDatasource and copy those two definitions as is.

For me in beta7, the UITableViewDatasource has

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

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

My implementation:

var items = ["Apple", "Pear", "Banana"]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
    cell.textLabel?.text = items[indexPath.row]
    cell.detailTextLabel?.text = "Test"
    return cell

}

Solution 5

Usee These methods: There is change in Data source methods-

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

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


protocol UITableViewDataSource : NSObjectProtocol {

    ****func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

    optional func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented

    optional func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
    optional func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?

    // Editing

    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    optional func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Moving/reordering

    // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
    optional func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Index

    optional func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! // return list of section titles to display in section index view (e.g. "ABCD...Z#")
    optional func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int // tell table which section corresponds to section title/index (e.g. "B",1))

    // Data manipulation - insert and delete support

    // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
    // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
    optional func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

    // Data manipulation - reorder / moving support

    optional func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
}

Ur code will works!!

Share:
31,958
Daddy
Author by

Daddy

i like programming!

Updated on July 14, 2022

Comments

  • Daddy
    Daddy almost 2 years

    Trying to conform to UITableViewDataSource and UITableViewDelegate inside a Swift UIViewController subclass.

    class GameList: UIViewController {
    
        var aTableView:UITableView = UITableView()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            aTableView.delegate = self
            aTableView.dataSource = self
            self.view.addSubview(aTableView)
            //errors on both lines for not conforming
        }
    
    }
    

    Docs say you should conform on the class line after the : but that's usually where the superclass goes. Another : doesn't work. Using a comma separated list after the superclass also doesn't work

    EDIT:

    Also must adopt all required methods of each protocol, which I wasn't initially doing.