stiller_leser
stiller_leser

Reputation: 1572

Fill tableviews in view with data, swift2

I am new to Swift and iOS development and am currently working on an app, that displays contact details. Now this app has a ViewController, that displays the contact details. Int he view, that belongs to that ViewController I have two table views, that should display additional information. To be able to address those table views I made the view controller a delegate of

UITableViewDataSource, UITableViewDelegate

and added the following methods:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(tableView)
    print(self.hobbiesTableView)
    if(tableView == self.hobbiesTableView){
        return card.hobbies.count
    } else {
        return card.friends.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("hobbycell", forIndexPath: indexPath) as UITableViewCell

    let row = indexPath.row

    if(tableView == self.hobbiesTableView){
        cell.textLabel?.text = card.hobbies[row]
    } else {
        cell.textLabel?.text = card.friends[row].vorname
    }

    return cell
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true;
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: edit the row at indexPath here
    }
    editAction.backgroundColor = UIColor.blueColor()


    let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    deleteAction.backgroundColor = UIColor.redColor()

    return [editAction,deleteAction]
}

However none of these methods get ever called. First question thus is what am I missing? Second question: Would the way I enabled editing allow for the removal of entries in those table views?

Upvotes: 0

Views: 53

Answers (1)

Stephen Darlington
Stephen Darlington

Reputation: 52565

You need to set the delegate and dataSource properties of your table views to be your view controller.

Upvotes: 1

Related Questions