【发布时间】:2016-10-26 10:36:54
【问题描述】:
我正在使用表格视图。有时我的数据会出错,因为它是空数据。当我得到空数据时,我想返回 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
}
【问题讨论】:
-
不明白你的问题到底出在哪里?
-
当我的数据计数为零时,我正在尝试手动返回单元格的编号并返回代码上方的数据。但它不起作用。我想我不能那样做。
-
我没有看到您尝试手动返回单元格编号的代码。我只看到检查数据是否为空数组,而不是用空数据内容填充它。
标签: ios xcode swift tableview tableviewcell