ios – 当前单元格不可见时,UICollectionView显示错误的项目数?

ios – 当前单元格不可见时,UICollectionView显示错误的项目数?,第1张

概述我有一个关于我的UICollectionView中显示的项目的问题,我不知道如何解释,但我会尽可能详细,希望有人可以帮助解决这个问题. 目前,我有一个包含UICollectionView的UITableViewCell.每个UICollectionCell包含一个从其indexPath中检索的图像. 以下示例代码是我对包含UICollectionView的UITableViewCell的实现: e 我有一个关于我的UICollectionVIEw中显示的项目的问题,我不知道如何解释,但我会尽可能详细,希望有人可以帮助解决这个问题.

@H_502_8@

目前,我有一个包含UICollectionVIEw的UItableVIEwCell.每个UICollectionCell包含一个从其indexPath中检索的图像.@H_502_8@

以下示例代码是我对包含UICollectionVIEw的UItableVIEwCell的实现:@H_502_8@

@H_502_8@

extension MainVIEwController: UItableVIEwDelegate{    func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell    {        var cell: UItableVIEwCell = UItableVIEwCell()        ....        if indexPath.section == 4        {            if let customCell = tableVIEw.dequeueReusableCell(withIDentifIEr: "CustomCell",for: indexPath) as? CustomtableVIEwCell            {                if images_ARRAY.isEmpty == false                {                    customCell.images_ARRAY = images_ARRAY                    customCell.awakeFromNib()                }                return imagesCell            }        }        return cell    }}class CustomtableVIEwCell: UItableVIEwCell{    @IBOutlet var imagesCollectionVIEw: UICollectionVIEw!    var images_ARRAY = [UIImage]()    var images = [INSPhotoVIEwable]()    overrIDe func awakeFromNib()    {        super.awakeFromNib()        // Initialization code        print("awakeFromNib")        // Through through each image of the record        for image in images_ARRAY        {            images.append(INSPhoto(image: image,thumbnailImage: SomeClass.resized(originalimage: image,withPercentage: 0.3) ) )        }        imagesCollectionVIEw.dataSource = self        imagesCollectionVIEw.delegate = self    }    ....    overrIDe func prepareForReuse()    {        super.prepareForReuse()        print("prepareForReuse")        images.removeAll()    }}extension CustomtableVIEwCell: UICollectionVIEwDataSource{    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int    {        return images.count    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell    {        let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: "CollectionVIEwCell",for: indexPath) as! ExampleCollectionVIEwCell        print("indexPath.row = \(indexPath.item)")        cell.populateWithPhoto(images[indexPath.item])        return cell    }}

我在我的MainVIEwController中使用UIImagePickerController来选择一个图像,在UICollectionVIEwCell中设置它,并在UItableVIEwCell中显示它,如下所示:@H_502_8@

@H_502_8@

extension MainVIEwController: UIImagePickerControllerDelegate{    func imagePickerController(_ picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : Any])    {        if let selectedImage = info[UIImagePickerControllerOriginalimage] as? UIImage        {            // Store and display the image            if let cell = mainVCtableVIEw.cellForRow(at: IndexPath(row: 0,section: 4) ) as? CustomtableVIEwCell            {                cell.images_ARRAY.append(selectedImage)                // Remove any existing images before adding all images again                cell.images.removeAll()                cell.awakeFromNib()                cell.imagesCollectionVIEw.reloadData()                images_ARRAY = cell.images_ARRAY                if ( (images_ARRAY.count - 1) % 4) == 0 &&                        images_ARRAY.count != 1                {                    self.mainVCtableVIEw.reloadRows(at: [ IndexPath(row: 0,section: 4) ],with: UItableVIEwRowAnimation.none)                }            }        }        picker.dismiss(animated: true,completion: nil)    }    func imagePickerControllerDIDCancel(_ picker: UIImagePickerController)    {        picker.dismiss(animated: true,completion: nil)    }}

每次用户选择图像时,图像阵列中的所有图像都将被删除,并在重新加载imagesCollectionVIEw之前从images_ARRAY重新添加.当添加4个图像时,UItableVIEwCell看起来像这样:@H_502_8@

@H_502_8@@H_502_8@

添加第5个图像时,UItableVIEwCell会动态更改其高度,因此会自动更改self.mainVCtableVIEw.reloadRows代码,只要添加了第9,13,17等图像,就会重新加载行以扩展UItableVIEwCell的高度:@H_502_8@

@H_502_8@@H_502_8@

我遇到的问题是当添加第9个图像时,单元格在屏幕上不可见,因此当调用self.mainVCtableVIEw.reloadRows(最终在我的CustomtableVIEwCell中调用cellForItemAt)时,indexPath.item最初报告为4,然后从0,1,2等开始.@H_502_8@

以下2个屏幕截图是将第8(左)和第9(右)图像添加到UICollectionVIEw时:@H_502_8@

@H_502_8@

@H_502_8@@H_502_8@

正如你在右边看到的那样,第6,第7和第8个图像已经消失,我有一个空行空间,第9个图像应该是.@H_502_8@

向下滚动表格视图显示:@H_502_8@

@H_502_8@@H_502_8@

添加第9个图像时打印出indexPath.item会显示以下内容:@H_502_8@

@H_502_8@

indexPath.row = 4indexPath.row = 0indexPath.row = 1indexPath.row = 2indexPath.row = 3indexPath.row = 4indexPath.row = 5indexPath.row = 6indexPath.row = 7

但是,一旦我添加了第10张图像,当重新加载imagesCollectionVIEw时,所有图像都会重新出现:@H_502_8@

@H_502_8@@H_502_8@

添加第10张图片后打印出来显示:@H_502_8@

@H_502_8@

indexPath.row = 0indexPath.row = 1indexPath.row = 2indexPath.row = 3indexPath.row = 4indexPath.row = 5indexPath.row = 6indexPath.row = 7indexPath.row = 8indexPath.row = 9

我不太明白发生了什么?我为这篇长篇文章道歉,但希望具体解决我的问题并展示插图,以便有人能够更好地理解我的帮助.@H_502_8@

谢谢!@H_502_8@解决方法 归功于@Aravind A R的建议,但问题最终通过在返回单元格之前重新加载cellForRowAt内的UICollectionVIEw来解决:

@H_502_8@

@H_502_8@

if images_ARRAY.isEmpty == false{    customCell.images_ARRAY = images_ARRAY    customCell.awakeFromNib()    customCell.imagesCollectionVIEw.reloadData()}
总结

以上是内存溢出为你收集整理的ios – 当前单元格不可见时,UICollectionView显示错误的项目数?全部内容,希望文章能够帮你解决ios – 当前单元格不可见时,UICollectionView显示错误的项目数?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1032247.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存