Andrew Andrew - 4 months ago 22
Swift Question

Two custom tableViewCells in UITableView

I am trying to create a contacts page where you can see all your contacts with a friend request cell showing up when you receive a friend request, but not there when you do not have any. At the moment, both custom cells work fine. The issue I have is that the contactRequestTableViewCell overlaps the first cell of the contactListTableViewCell.

I have researched other questions about two custom tableviewcells and none are quite having the same issues that I am facing.

enter image description here

This is what my storyboard looks like

Here is my executing code at the moment, I am returning 2 sections in the table view.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactListTableViewCell
let requestCell = tableView.dequeueReusableCellWithIdentifier("requestCell", forIndexPath: indexPath) as! ContactRequestsTableViewCell


let user = OneRoster.userFromRosterAtIndexPath(indexPath: indexPath)


if (amountOfBuddyRequests > 0) {


if (indexPath.section == 0) {

requestCell.hidden = false
cell.hidden = false
requestCell.friendRequestLabel.text = "test"


} else if (indexPath.section >= 1) {

cell.contactNameLabel!.text = user.displayName;
cell.contactHandleLabel!.text = "@ " + beautifyJID(user.jidStr)
cell.contactHandleLabel!.textColor = UIColor.grayColor()

OneChat.sharedInstance.configurePhotoForImageView(cell.imageView!, user: user)
}

return cell;
}

else { // if buddy requests == 0
requestCell.hidden = true



cell.contactNameLabel!.text = user.displayName;
cell.contactHandleLabel!.text = "@ " + beautifyJID(user.jidStr)
cell.contactHandleLabel!.textColor = UIColor.grayColor()

print ("This is how many unreadMessages it has \(user.unreadMessages)")

// If there is unread messages for a person highlight it blue
// However this feature isn't working right now due to unreadMessages bug
if user.unreadMessages.intValue > 0 {
cell.backgroundColor = .blueColor()
} else {
cell.backgroundColor = .whiteColor()
}

OneChat.sharedInstance.configurePhotoForCell(cell, user: user)

return cell;
}


}


enter image description here

This is the current output that I have right now, my cells that have "test" are covering up other contactListTableViewCells.

Answer

It turns out that the issue was dealing with the data sources. My data sources were not pointing to the correct tableviewcell. This resulted in them pointing to an incorrect cell. This issue was fixed by remaking the data sources system that was in place. This issue will not affect most as the data sources will point to the correct tableviewcell by default.

Contrary to what another poster said, you can in fact display two or more custom cells in a single table. This is how I fixed the tableView display issues:

var friendRequests = ["FriendRequest1", "FriendRequest2"]
var contacts = ["User1","User2","User3","User4"]
var amountOfBuddyRequests = 1


 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if (amountOfBuddyRequests > 0) {
        return 2
    }

    return 1
}




override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (amountOfBuddyRequests > 0) {
        if (section == 0) {
            return friendRequests.count
        }

    }
  return contacts.count




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



    if (amountOfBuddyRequests > 0) {
        if (indexPath.section == 0) {
            let requestCell = tableView.dequeueReusableCellWithIdentifier("requestCell") as! ContactRequestsTableViewCell

            requestCell.friendRequestLabel.text = friendRequests[indexPath.row]
            requestCell.onButtonTapped = {
                self.friendRequests.removeAtIndex(indexPath.row)
                self.tableView.reloadData()
            }
            requestCell.addButtonTapped = {
                self.addUser(self.friendRequests[indexPath.row])
                self.friendRequests.removeAtIndex(indexPath.row)
                self.tableView.reloadData()
            }


            return requestCell
        }
    }


    let friendCell = tableView.dequeueReusableCellWithIdentifier("FriendCell") as! ContactListTableViewCell
    friendCell.contactNameLabel.text = contacts[indexPath.row]




    return friendCell
}