Ameya Vichare
Ameya Vichare

Reputation: 1289

Populating two types of UITableViewCell in a single UITableView

I have a ViewController called notificationsVC with two tableViews - notifTable and messageTable. In notifTable I have two types of UITableViewCells -

enter image description here

As you can see there's a followed you and commented/liked your post patterns. Until now I have been using two UITableViewCell's like this. (reuse identifier - cell1 and cell3)

enter image description here

The first one I made a coach touch file named notification_cell.swift and another notification2_cell.swift

notification_cell.swift

import UIKit
class notification_cell: UITableViewCell {    
    @IBOutlet weak var profilePic: UIImageView!    
    @IBOutlet weak var username: UILabel!    
    @IBOutlet weak var followNotif: UILabel! 
}

notification2_cell.swift

import UIKit
class notification2_cell: UITableViewCell {    
    @IBOutlet weak var profilePic: UIImageView! 
    @IBOutlet weak var username: UILabel!    
    @IBOutlet weak var c_l_notif: UILabel!    
    @IBOutlet weak var postImage: UIImageView!
}

I have four arrays in all -

var username = [String]()
var notif = [String]()
var u_id = [String]()
var p_id = [String]()

For eg, when I do a API call I get this

username-> ["Anton Griezmann", "Anonymous", "Anonymous"]
u_id-> ["2", "30", "31"]
notif-> ["followed you", "liked your post", "liked your post"]
p_id-> ["", "9", "9"]

What I'm trying to do is whenever there's a blank "" in p_id I know I have to initialise cell1 and otherwise cell3

This is my code for cellForRowAtIndexpath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell{
        var cell = UITableViewCell()
        var index = [Int]()

        if(tableView==self.notifTable)
        {
            for i in 0..<p_id.count {
                if p_id[indexPath.row].isEmpty {
                    index.append(i)

                //cell for followed you
                let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! notification_cell
                    cell.username.text = username[indexPath.row]
                    cell.followNotif.text =
                    notif[indexPath.row]
                    return cell

                }
                else {

                    //cell for commented/liked
                    let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as! notification2_cell
                    cell.username.text = username[indexPath.row]
                    cell.c_l_notif.text = notif[indexPath.row]
                    return cell

                }

            }

        }
        else
        {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
            cell.textLabel?.text = "hola"

            return cell

        }


}

What I get when I run it is this

enter image description here

That is the first cell is getting overwritten again. I need a way to find out which cell should be initialised at what indexPath but can't think of how. Any suggestions are welcome!

Upvotes: 2

Views: 89

Answers (1)

Nirav D
Nirav D

Reputation: 72410

First of all you are returning wrong cell in your cellForRowAtIndexPath try to return cell inside the if - else block, and if you want to check for p_id[indexPath.row] is "" means it is empty so you can check its length or use isEmpty function of String, There is no need to go through a loop, Also.

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

        if(tableView==self.notifTable)
        {
            if (p_id[indexPath.row].isEmpty) {

                //cell for followed you
                let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! notification_cell
                cell.username.text = username[indexPath.row]
                cell.followNotif.text =
                    notif[indexPath.row]
                return cell
            }
            else {

                //cell for commented/liked
                let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as! notification2_cell
                cell.username.text = username[indexPath.row]
                cell.c_l_notif.text = notif[indexPath.row]
                return cell
            }
        }
        else
        {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
            cell.textLabel?.text = "hola"

            return cell

        }            
}

Upvotes: 2

Related Questions