Swift:当数据为空时如何填充单元格?

编程入门 行业动态 更新时间:2024-10-27 08:24:59
本文介绍了Swift:当数据为空时如何填充单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用表格视图.有时我的数据出错,因为它是空数据.当我得到空数据时,我想返回 1 个或多个单元格并显示空数据信息文本,如无数据可用!".

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!".

代码:

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
}

0 之后,所有数据都为空.我正在尝试返回单元格,并且在 cellForRowAtIndexPath 中使用此代码:

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
}

感谢 Sethmr,我找到了解决方案并做到了.这是我的工作代码:

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
        }
    }
}

此外,我从顶部移动了 cellForRowAtIndexPath 中的 item 值并且它起作用了.我在它不为空时调用了该项目.

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
}

推荐答案

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
    }
}  

这似乎是您想要的.

这篇关于Swift:当数据为空时如何填充单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:09:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   单元格   数据   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!