问题描述
限时送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:关键词]
发布评论