Channel List

The ChannelListViewController contains a simple TableView that shows a list of Channels. This section will only deal with the essential functionality and not styling.

Set up the TableView like so in your viewDidLoad:


// This is the data backing the TableView
var channels = [ParticipatedChannel]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Get the AppDelegate which contains the Mitter object
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
    // TableView setup
    ...
    
    // Fetch the list of channels and display
    appDelegate.mitter.channels.getChannelsForCurrentUser {
        result in
        switch result {
        case .success(let fetchedChannels):
            self.channels = fetchedChannels
            print("Received all channels for logged in user")
            self.tableView.reloadData()

        case .error:
            print("Couldn't fetch all channels for logged in user")
        }
    }
}

To render the cell, modify the UITableViewDelegate function as follows:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // create a new cell if needed or reuse an old one
        let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

        // set the text from the data model
        cell.textLabel?.text = self.channels[indexPath.row].channel.channelId

        return cell
    }

To open the ChannelWindowViewController on clicking a cell:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let channelWindowViewController = storyboard.instantiateViewController(withIdentifier: "channelWindowView") as! ChannelWindowViewController
        channelWindowViewController.channelId = channels[indexPath.row].channel.channelId
        navigationController!.pushViewController(channelWindowViewController, animated: true)
    }

Last updated