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

发布于 2025-01-10 05:22:56 字数 2253 浏览 0 评论 0原文

我有一个有 4 个单元格的 tableView,在第二个和第四个单元格中,我添加了集合视图。问题是,在第二个单元格中,当数组计数为 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()

这段代码有什么问题。

I have a tableView that has 4 cells and in the second and fourth cells, I add collection view. the problem is that in the second cell when the array count is 1 the collection view does not get called. as soon as array count is greater than 1 the collection view shows data accordingly.

I am doing this for 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
}

And this for collection view 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()

what is wrong in this code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-01-17 05:22:56

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

根据您当前的逻辑,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
}

Update

您得到的输出与使用 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 时初始化集合视图

When the array count is 1, the max indexPath row will be 0, hence this condition will be false if indexPath.row == 1 and so your collection view will not be created.

With your current logic, the minimum array size for a collectionview to show is 2.

Finally, a couple of nice suggestions by Duncan, the code would be cleaner if you used a switch rather than an if-else block and also you need to take care of scenarios where indexPath.row > 3, something like:

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
}

Update

The output you get has nothing to do with using an if or a switch but with the logic of your implementation.

An array's index starts at 0, so the first element in an array is at index 0.

var array = [5, 6, 10, 11, 14]

// this will print 5 because an array's index starts at `0`
print(array[0]) 

Similarly, a UITableView's row and section, starts at 0, not at 1.

When you check indexPath.row == 0, you are not checking if the table has 0 rows, you are checking if the current cell is in row 0 (which is the first row)

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

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

So when your array has a count of 1, the following happens

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
}

If you want to see the collection view when the array size is 1, you need to initialize the collection view when indexPath.row == 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文