Reputation: 61
I am trying to test my friendTableView with a sample array of friends. I have created a custom cell class to show a profile picture and a label for the friends' names. However, when I run the app, the table view is completely empty. Here is my view controller class:
import UIKit
class FriendsVC: UIViewController {
//var friends: [Friend] = User.current.friends
var friends = [Friend]()
var groups: [Group] = []
@IBOutlet weak var friendTableView: UITableView!
@IBOutlet weak var groupTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
friends = [Friend(uid: "aslfkjlaskdfj", firstName: "Jane", lastName: "Johnson", phone: "+18003473874", available: true), Friend(uid: "asdfkhkjshkjh", firstName: "Bob", lastName: "Odenkirk", phone: "+18003474874", available: true), Friend(uid: "aslfkjlaskdfj", firstName: "Walt", lastName: "White", phone: "+18003273874", available: true)]
// Do any additional setup after loading the view.
friendTableView.delegate = self
friendTableView.dataSource = self
}
My extension:
extension FriendsVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return min(User.current.friends.count, Constants.maximumFriendsDisplayed)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// if tableView == friendTableView {
let friend = friends[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.Identifiers.friendCell, for: indexPath) as! FriendCell
cell.setFriend(friend: friend)
return cell
And my cell model class:
import UIKit
import InitialsImageView
class FriendCell: UITableViewCell {
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var friendNameLabel: UILabel!
func setFriend(friend: Friend) {
if let pic = friend.profilePic {
self.profilePic.image = pic
} else {
self.profilePic.setImageForName(friend.firstName + friend.lastName, backgroundColor: nil, circular: true, textAttributes: nil)
}
friendNameLabel.text = friend.firstName + " " + friend.lastName
}
}
I'm not getting any errors, just an empty table view. what am I doing wrong?
Upvotes: 1
Views: 55
Reputation: 14397
You are not reloading TableView
override func viewDidLoad() {
super.viewDidLoad()
friends = [Friend(uid: "aslfkjlaskdfj", firstName: "Jane", lastName: "Johnson", phone: "+18003473874", available: true), Friend(uid: "asdfkhkjshkjh", firstName: "Bob", lastName: "Odenkirk", phone: "+18003474874", available: true), Friend(uid: "aslfkjlaskdfj", firstName: "Walt", lastName: "White", phone: "+18003273874", available: true)]
// Do any additional setup after loading the view.
friendTableView.delegate = self
friendTableView.dataSource = self
friendTableView.reloadData()
}
Try to return friends.count
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
Upvotes: 1