Zohaib ZJ
Zohaib ZJ

Reputation: 89

How i can remove the empty cells from my table view

I'm using custom cells for table view and as per my code if no condition met it adds an empty cell or row in my table view. i'm new on iOS please help me in removing empty cells. here is my code snippet.

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

        let dict1 = arrMsg.object(at: indexPath.row) as! NSDictionary

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChatTableViewCell
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "Cell2") as! Chat2TableViewCell

        if((String(describing: dict1.object(forKey: "SenderId")!)) == Auth.auth().currentUser?.uid && (ChatVC.selectedUser.replacingOccurrences(of: ".", with: "") == (String(describing: dict1.object(forKey: "ReceiverId")!))))
        {
            cell2.lblSender.text = (dict1.object(forKey: "Message") as! String)
            cell2.lblSender.backgroundColor = UIColor(red: 0.09, green: 0.54, blue: 1, alpha: 1)
            cell2.lblSender.font = UIFont.systemFont(ofSize: 18)
            cell2.lblSender.textColor = .white
            cell2.lblSender?.layer.masksToBounds = true
            cell2.lblSender.layer.cornerRadius = 7
            return cell2
        }
        else if ((String(describing: dict1.object(forKey: "ReceiverId")!)) == (Auth.auth().currentUser!.email?.replacingOccurrences(of: ".", with: ""))!)
        {
            cell.lblReceiver.text = (dict1.object(forKey: "Message") as! String)
            cell.lblReceiver.backgroundColor = UIColor .lightGray
            cell.lblReceiver.font = UIFont.systemFont(ofSize: 18)
            cell.lblReceiver.textColor = UIColor.white
            cell.lblReceiver?.layer.masksToBounds = true
            cell.lblReceiver.layer.cornerRadius = 7
            return cell
        }
        else {
            let cell = UITableViewCell()
            return cell
        }
    }

Upvotes: 0

Views: 191

Answers (3)

Mohammad Yunus
Mohammad Yunus

Reputation: 214

go to story board select your tableview in the inspect change separator style from default or whatever to none

Upvotes: 0

Tom Pearson
Tom Pearson

Reputation: 384

I think this is something that should be cleared up by your TableView's DataSource instead of you having to handle the logic of whether something exists or not in cellForRowAt. You should be able to add logic to find the number of rows in each section/how many sections there are based upon your arrMsg object

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need

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

    let dict1 = arrMsg.object(at: indexPath.row) as! [String:Any]
    let str = dict1["SenderId"] as! String
    if str == Auth.auth().currentUser?.uid { 
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "Cell2") as! Chat2TableViewCell
        cell2.lblSender.text = (dict1.object(forKey: "Message") as! String)
        cell2.lblSender.backgroundColor = UIColor(red: 0.09, green: 0.54, blue: 1, alpha: 1)
        cell2.lblSender.font = UIFont.systemFont(ofSize: 18)
        cell2.lblSender.textColor = .white
        cell2.lblSender?.layer.masksToBounds = true
        cell2.lblSender.layer.cornerRadius = 7
        return cell2
    }
    else
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChatTableViewCell
        cell.lblReceiver.text = (dict1.object(forKey: "Message") as! String)
        cell.lblReceiver.backgroundColor = UIColor .lightGray
        cell.lblReceiver.font = UIFont.systemFont(ofSize: 18)
        cell.lblReceiver.textColor = UIColor.white
        cell.lblReceiver?.layer.masksToBounds = true
        cell.lblReceiver.layer.cornerRadius = 7
        return cell
    }

}

it's your job to make sure that senderID should be the current user or the sending user , but not nil which will lead to returning the empty cell , also don't use NS stuff in Swift

Upvotes: 0

Related Questions