Sente Sente - 8 months ago 69
Swift Question

UITableView returning different cells

For a UITableView, I am using one of 2 different cells. The first one displays content and the second one is a simple label that says there is no content if the array is empty. I am also switching a segmented controller which controls which array to use. I have a class for both of the cells. The second one just has an IBoutlet for the label which will change based on the segmented controller. My code looks like this:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0

switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
returnValue = accounts.count
break
case 1:
returnValue = user.count
break
case 2:
returnValue = mutual.count
default:
break
}

return returnValue
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConnectCell", for: indexPath) as! ConnectTableViewCell

let cell2 = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell

switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
let user = self.accounts[indexPath.row]

cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self

break

case 1:
let user = self.user[indexPath.row]

cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self

break

case 2:
let user = self.mutual[indexPath.row]

cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self

break

default:
break
}

return cell
}


I just added "cell2" because this a new feature so far. I believe I have to use numberOfRowsInSection to check if the array is empty and return 1 if it is empty and then put a conditional in the cases in CellForRowAt to check if the array is empty and then use cell2. I can then change the text for cell2. However, I do not know how to return the single cell2 after the break. Any ideas?

Answer Source

I would do this using two sections. Use section 0 for the real data and section 1 for the "no content" cell.

The following only shows the 2nd section ("no content") if there are no rows in the 1st section.

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var returnValue = 0

    switch (mySegmentedControl.selectedSegmentIndex) {
    case 0:
        returnValue = accounts.count
    case 1:
        returnValue = user.count
    case 2:
        returnValue = mutual.count
    default:
        break
    }

    if section == 0 {    
        return returnValue
    } else {
        return returnValue == 0 ? 1 : 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ConnectCell", for: indexPath) as! ConnectTableViewCell

        switch(mySegmentedControl.selectedSegmentIndex) {
        case 0:
            let user = self.accounts[indexPath.row]
            cell.user = user
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            cell.delegate = self
        case 1:
            let user = self.user[indexPath.row]
            cell.user = user
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            cell.delegate = self
        case 2:
            let user = self.mutual[indexPath.row]
            cell.user = user
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            cell.delegate = self
        default:
            break
        }

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell

        switch(mySegmentedControl.selectedSegmentIndex) {
        case 0:
            cell.textLabel.text = "No accounts"
        case 1:
            cell.textLabel.text = "No users"
        case 2:
            cell.textLabel.text = "No mutual"
        default:
            break
        }

        return cell
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download