ttorbik
ttorbik

Reputation: 408

SWRevealViewController Cells show up blank until click

I am using SWRevealViewController, when I create the cell and run the app the side menu shows up blank. Once you click on one of the cells, however, the cell appears. The top larger cell still doesn't appear when you click on it though. Here is my code.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuTitle.count+3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let index = indexPath.row
    var defCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
    switch (index) {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell") as! profileCell
            cell.nameLbl.text = "Name Here"   //need to pull name from the database here
            cell.profileImg.image = UIImage(named: "face")! //pull from db
            print(cell)
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell") as! labelCell
            cell.menuLabel.text = "For Sale"
            return cell
        case 2,3,4:
            let cell = tableView.dequeueReusableCell(withIdentifier: "titleCells") as! titleCells
            cell.cellImg.image = iconImg[indexPath.row - 2]
            cell.cellName.text! = menuTitle[indexPath.row - 2]
            return cell
        case 5:
            let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell") as! labelCell
            cell.menuLabel.text = "Profile"
            //labelCell.darkLine //cahnge line color to orange
            return cell
        case 6,7,8,9,10,11:
            print(indexPath.row)
            let cell = tableView.dequeueReusableCell(withIdentifier: "titleCells") as! titleCells
            cell.cellImg.image = iconImg[indexPath.row - 3]
            cell.cellName.text! = menuTitle[indexPath.row - 3]
            print(menuTitle[indexPath.row - 3])
            return cell
        case 12:
            //change line color
            let cell = tableView.dequeueReusableCell(withIdentifier: "titleCells") as! titleCells
            cell.cellImg.image = iconImg[indexPath.row - 3]
            cell.cellName.text! = menuTitle[indexPath.row - 3]
            return cell
        case 13:
            let cell = tableView.dequeueReusableCell(withIdentifier: "titleCells") as! titleCells
            cell.cellImg.image = iconImg[indexPath.row - 3]
            cell.cellName.text! = menuTitle[indexPath.row - 3]
            return cell
        default:
            return defCell

    }

Any tips or different approaches other than a switch statement would be helpful. Thanks in advance.

Upvotes: 0

Views: 50

Answers (1)

Malik
Malik

Reputation: 3802

The problem is that when you go into any switch case and dequeue cell, there is already another cell dequeued in defCell. I have had trouble with it in the past. I don't know why but for some reason, it messes up the tableView. One way to fix it would be to do something like this

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell") as! profileCell
        cell.nameLbl.text = "Name Here"   //need to pull name from the database here
        cell.profileImg.image = UIImage(named: "face")! //pull from db
        print(cell)
        return cell
    } else if indexPath.row == 1 || indexPath.row == 5 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell") as! labelCell
        if indexPath.row == 1 {
            cell.menuLabel.text = "For Sale"
        } else {
            cell.menuLabel.text = "Profile"
        }
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "titleCells") as! titleCells
        if indexPath.row <= 4 {
            cell.cellImg.image = iconImg[indexPath.row - 2]
            cell.cellName.text! = menuTitle[indexPath.row - 2]
        } else {
            cell.cellImg.image = iconImg[indexPath.row - 3]
            cell.cellName.text! = menuTitle[indexPath.row - 3]
        }
        return cell
    }
}

As you can see, with this approach, there will never be more than one cell dequeued at a time

Upvotes: 1

Related Questions