当数组计数0集合视图不显示数据时



我有一个tableView,它有4个单元格,在第二个和第四个单元格中,我添加了集合视图。问题是,在第二个单元格中,当数组计数为1时,不会调用集合视图。一旦数组计数大于1,集合视图就会相应地显示数据。

我这样做是为了tableView-:

if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAllCell") as! ViewAllCell
cell.countLbl.text = String(historyArray.count)
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "VideoDownloadTableViewCell") as! VideoDownloadTableViewCell
cell.collectionViewOne.layoutSubviews()
cell.collectionViewOne.reloadData()
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "PlatformTableViewCell") as! PlatformTableViewCell
return cell
} else if indexPath.row == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "PlateformViewTableViewCell") as! PlateformViewTableViewCell
return cell
}

这是集合视图cellForRow-:

if collectionView.tag == 100 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoViewCell", for: indexPath) as! VideoViewCell
cell.cornerView.layer.cornerRadius = cell.cornerView.frame.size.width/2
cell.cornerView.clipsToBounds = true
cell.cornerView.layer.borderWidth = 2
return cell
}
if collectionView.tag == 101 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DashboardCollectionCell", for: indexPath) as! DashboardCollectionCell
if indexPath.row == 0 {
cell.socialPlatFormName.text = kConstant.plateformName.facebook
cell.socialPlatFormName.textColor = hexStringToUIColor(hex: "0858F2")
} else if indexPath.row == 1 {
cell.socialPlatFormName.text = kConstant.plateformName.instagram
cell.socialPlatFormName.textColor = hexStringToUIColor(hex: "D73F8C")
cell.socialPlatFormImage.image = UIImage(named: "instagram")
} else if indexPath.row == 3 {
cell.socialPlatFormName.text = kConstant.plateformName.snapChat
cell.socialPlatFormName.textColor = hexStringToUIColor(hex: "D6AE12")
cell.socialPlatFormImage.image = UIImage(named: "snapchat")
}
return cell
}
return UICollectionViewCell()

这段代码出了什么问题。

当数组计数为1时,最大indexPath行将为0,因此此条件将为falseif indexPath.row == 1,因此不会创建集合视图。

根据您当前的逻辑,collectionview显示的最小数组大小为2。

最后,Duncan提出了一些很好的建议,如果您使用switch而不是if-else块,代码会更干净,而且您还需要处理indexPath.row > 3的场景,比如:

switch indexPath.row
{
case 0:
// do what you want
case 1:
// set up collection view
case 2:
// do something else
case 3:
// do something else
default:
// handle cases where the array size is
// greater than 4
}

更新

您得到的输出与使用ifswitch无关,而是与实现的逻辑有关。

数组的索引从0开始,因此数组中的第一个元素位于索引0。

var array = [5, 6, 10, 11, 14]
// this will print 5 because an array's index starts at `0`
print(array[0]) 

类似地,UITableView的行和节从0开始,而不是从1开始。

检查indexPath.row == 0时,不是检查表是否有0行,而是检查当前单元格是否在第0行(第一行(

// this is the first row in your table view
indexPath.row == 0 
// this is the second row in your table view
indexPath.row == 1 

因此,当你的数组计数为1时,会发生以下情况

switch indexPath.row
{
case 0:
// This gets executed when array size is at least 1
// Since the array size is 1, this case gets executed

case 1:
// This only gets executed when array size is at least 2
// Executing this case means the table has at least 2 rows
// Since array size is 1, table view won't have a 2nd row
// Not executed since the array size is 1
// Hence, you won't see the collection view

case 2:
// This case only gets executed when array size is at least 3
// Executing this case means the table has at least 3 rows
// Not executed since the array size is 1

case 3:
// This case gets executed when array size is at least 4
// Executing this case means the table has at least 4 rows
// Not executed since the array size is 1

default:
// This gets executed when array size is at least 5
// Executing this case means the table has more than 4 rows
// Not executed since the array size is 1
}

如果您想在数组大小为1时查看集合视图,则需要在indexPath.row == 0时初始化集合视图

最新更新