Swift : How can I fill the cell when the data is empty?

浪尽此生 提交于 2020-01-06 03:37:06

问题


I'm using tableview. Sometimes my datas getting error, because it is empty data. When I get empty data, I want to return 1 or more cell and show empty data information texts like "No data available!".

Code :

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

        if segmentControl.selectedIndex == 0 {
            a = 8
            return a
        }else if segmentControl.selectedIndex == 1{
            a = 4
            return a
        }else if segmentControl.selectedIndex == 2{
            a =  2
            return a
        }else if segmentControl.selectedIndex == 3{
            a = 1
            return a
        }

    return a
}

After then 0, all datas are empty. I'm trying to return cell and I'm using this code in cellForRowAtIndexPath :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("knockoutsCell", forIndexPath: indexPath) as! KnockoutsTableViewCell

    let item = knockoutsTableArray[indexPath.row]
    if knockoutsTableArray.count == 0{
        cell.dateLabel.text = "Date"
        cell.homeImage.image = UIImage(named: "imagePic.png")
        cell.homeLabel.text = "Empty data!"

    }else{
    if item.playing == true{
        cell.dateLabel.text = item.date
        cell.homeImage.image = UIImage(named: "\(item.homeName)")
    }
    }

    return cell
}

I found the solution and did it thanks to Sethmr. Here is my working code :

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if knockoutsTableArray.count != 0 {
        return knockoutsTableArray.count
    } else{
        if segmentControl.selectedIndex == 0{
            return 8
        }else if segmentControl.selectedIndex == 1{
            return 4
        }else if segmentControl.selectedIndex == 2{
            return 2
        }else{
            return 1
        }
    }
}

Also I moved the item value in cellForRowAtIndexPath from the top and It worked. I called the item when It's not empty.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("knockoutsCell", forIndexPath: indexPath) as! KnockoutsTableViewCell

    if knockoutsTableArray.count == 0{
        cell.dateLabel.text = "Date"
        cell.homeImage.image = UIImage(named: "imagePic.png")
        cell.homeLabel.text = "Empty data!"

    }else{
    let item = knockoutsTableArray[indexPath.row]
    if item.playing == true{
        cell.dateLabel.text = item.date
        cell.homeImage.image = UIImage(named: "\(item.homeName)")
    }
    }

    return cell
}

回答1:


I am a big fan of switch statements over if else for readability.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch segmentControl.selectedIndex {
    case 0:
        if knockoutArray.count != 0 {
             return knockoutArray.count
        } else {
             return 1
        } 
    case 1:
        return 4
    case 2:
        return 2
    case 3:
        return 1
    default:
        print("error with selectedIndex")
        return 0
    }
}

If all the rows are empty it is because one of the following: Your segmentControl.selectedIndex = -1 becuase all of itself is unselected or the segmented controller is nil. You might not have called tableView.reloadData() after the point in time that this case had changed.

Show me all of cellForRowAtIndexPath, more info about myArray, and anything else that you think could be helpful and I will see what I can do.




回答2:


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch segmentControl.selectedIndex {
    case 0:
        if knockoutsTableArray.count != 0 {
            return knockoutsTableArray.count
        } else {
            return 1
        } 
    case 1:
        if knockoutsTableArray.count != 0 {
            return 4
        } else {
            return 8
        } 
    case 2:
        return 2
    case 3:
        return 1
    default:
        print("error with selectedIndex")
        return 0
    }
}  

This seems like what you want.



来源:https://stackoverflow.com/questions/38002707/swift-how-can-i-fill-the-cell-when-the-data-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!