当数组计数为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当数组计数为 1 时,最大 indexPath 行将为 0,因此如果
if indexPath.row == 1
此条件将为 false,因此不会创建集合视图。根据您当前的逻辑,collectionview 显示的最小数组大小是 2。
最后,Duncan 提出了一些很好的建议,如果您使用
switch
而不是,代码会更干净if-else
块,并且您还需要注意indexPath.row > 的情况3
,类似于:Update
您得到的输出与使用
if
或switch
无关,而是与逻辑有关您的实施。数组的索引从 0 开始,因此数组中的第一个元素位于索引 0 处。
类似地,UITableView 的行和部分从
0
开始,而不是从 1 开始。当您检查
indexPath.row == 0
,您不是在检查表是否有 0 行,而是在检查当前单元格是否位于第 0 行(即第一行)因此,当您的数组有计数为 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 anif-else
block and also you need to take care of scenarios whereindexPath.row > 3
, something like:Update
The output you get has nothing to do with using an
if
or aswitch
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.
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)So when your array has a count of 1, the following happens
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