gpt4 book ai didi

ios - swift : How can I fill the cell when the data is empty?

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:35 25 4
gpt4 key购买 nike

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

代码:

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 中使用此代码:

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 值,它起作用了。当它不为空时,我调用了该项目。

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
}

最佳答案

为了可读性,我非常喜欢 switch 语句而不是 if else。

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

如果所有行都是空的,那是因为以下原因之一:您的 segmentControl.selectedIndex = -1 因为它本身未被选中或分段 Controller 为零。在这种情况发生变化的时间点之后,您可能没有调用 tableView.reloadData()。

向我显示所有 cellForRowAtIndexPath、有关 myArray 的更多信息以及您认为可能有用的任何其他信息,我会看看我能做什么。

关于ios - swift : How can I fill the cell when the data is empty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38002707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com