mitter.io
Search…
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:
1
2
// This is the data backing the TableView
3
var channels = [ParticipatedChannel]()
4
5
override func viewDidLoad() {
6
super.viewDidLoad()
7
8
// Get the AppDelegate which contains the Mitter object
9
let appDelegate = UIApplication.shared.delegate as! AppDelegate
10
11
// TableView setup
12
...
13
14
// Fetch the list of channels and display
15
appDelegate.mitter.channels.getChannelsForCurrentUser {
16
result in
17
switch result {
18
case .success(let fetchedChannels):
19
self.channels = fetchedChannels
20
print("Received all channels for logged in user")
21
self.tableView.reloadData()
22
23
case .error:
24
print("Couldn't fetch all channels for logged in user")
25
}
26
}
27
}
Copied!
To render the cell, modify the UITableViewDelegate function as follows:
1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2
3
// create a new cell if needed or reuse an old one
4
let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
5
6
// set the text from the data model
7
cell.textLabel?.text = self.channels[indexPath.row].channel.channelId
8
9
return cell
10
}
Copied!
To open the ChannelWindowViewController on clicking a cell:
1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
2
let storyboard = UIStoryboard(name: "Main", bundle: nil)
3
let channelWindowViewController = storyboard.instantiateViewController(withIdentifier: "channelWindowView") as! ChannelWindowViewController
4
channelWindowViewController.channelId = channels[indexPath.row].channel.channelId
5
navigationController!.pushViewController(channelWindowViewController, animated: true)
6
}
Copied!
Copy link