滚动时选择了多个单元格[重复使用单元格问题] [英] multiple cells selected on scrolling [reuse cells problem]

查看:124
本文介绍了滚动时选择了多个单元格[重复使用单元格问题]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个collectionViewCell,其中包含图像和标签.当我选择一个单元格并向前滚动时,我发现正在选择其他单元格.同样,当我向后滚动时,我会找到另一个单元格,而不是选择我所选择的单元格.

I have a collectionViewCell which contains an image and a label. When I select a cell and scroll ahead I find other cell being selected. Also when I scroll back I find the other cell and not the cell I selected is selected.

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return followedUsers.count
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = attachProfilesCollectionView.dequeueReusableCell(withReuseIdentifier: "attachCells", for: indexPath) as? attachUsersCell
    cell!.subscribedUserId = self.followedUsers[indexPath.row].userId
    cell?.profileNameToAttah.text = self.followedUsers[indexPath.row].fullName
    cell?.profileImageToAttch.loadImagesWithUrl(from: self.followedUsers[indexPath.row].ImagePath)



    if mLastSelectedIndex == indexPath.row {
    cell?.isSelected = true}
    else{cell?.isSelected = false}

    return cell!
}




func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 80, height: 110)
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         let cell = attachProfilesCollectionView.cellForItem(at: indexPath) as! attachUsersCell
                cell.subscribedUserId = following[indexPath.item]
                usersToAttach.append(cell.subscribedUserId)
                attachedCounter += 1
                attachCounterFun()
                print(usersToAttach)
    cell.checkMarkImage.isHidden = false
    attachCounterFun()
    guard mLastSelectedIndex != indexPath.row else{return}
    mLastSelectedIndex = indexPath.row
    print("this is  addition \(usersToAttach)")
}




func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  let cell_ = attachProfilesCollectionView.cellForItem(at: indexPath) as! attachUsersCell
    let toRemove = usersToAttach.index(of: following[indexPath.row])
    usersToAttach.remove(at: toRemove!)
    attachedCounter -= 1
     attachCounterFun()
    print(usersToAttach)
    cell_.checkMarkImage.isHidden = true
    print("this is removal \(usersToAttach)")
}


func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if isSelected {
        cell.isSelected = true}
    else { isSelected = false }
}

目前,我已启用分页,并且几乎已启用分页,但是偶尔选中的单元格会在滚动时更改选择

At present I have enabled paging and almost there but occasionally the selected cells change selection upon scroll

推荐答案

在控制器中维护全局变量

Maintain global variable in your controller

var mLastSelectedIndex = -1

每当调用cellForRowAtIndexPath方法时,检查是否选择了当前单元格.如果选择了此选项,则更新其UI.我建议您仅更改单元格类上的选定单元格UI.这样,您可以轻松管理单元窃听并帮助您编写简洁的代码.

Whenever cellForRowAtIndexPath method called check if current cell is selected or not .If this is selected One update its UI. I suggest you to change selected cell UI on cell class only. This way you can easily manage cell tapping and helps you writing clean code.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  let cell = attachProfilesCollectionView.dequeueReusableCell(withReuseIdentifier: "attachCells", for: indexPath) as? attachUsersCell
  cell!.subscribedUserId = self.followedUsers[indexPath.row].userId
  cell?.profileNameToAttah.text = self.followedUsers[indexPath.row].fullName
  cell?.profileImageToAttch.loadImagesWithUrl(from:   self.followedUsers[indexPath.row].ImagePath)
// here update selected index path
  if mLastSelectedIndex == indexPath.row {
      cell?.isSelected = true
  }
else{ 
    cell?.isSelected = false
  }

   return cell!
}

更新点击的单元格上的最后选择的索引.

Update last selected index on cell tapped.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    guard mLastSelectedIndex != IndexPath.row else{return}
    let indexpath = IndexPath(row:mLastSelectedIndex, section: 0)
    mColllectionView.cellForItem(at: indexpath)?.isSelected = !mColllectionView.cellForItem(at: indexpath)!.isSelected
    mLastSelectedIndex = IndexPath.row 
  }

在您的 attachUsersCell.swift 中声明cell的 isSelected 属性.此属性的默认值为false,表示未选中该单元格.

In your attachUsersCell.swift declare isSelected property of cell . The default value of this property is false, which indicates that the cell is not selected.

override var isSelected: Bool{
    didSet{
        if isSelected {

          setSelectedUI()

        }
        else{

            setUnSelectedUI()

        }

    }
}

func setSelectedUI(){
     borderColor = UIColor.systemOrange
     borderWidth = 2
     cornerRadius = 8
      clipsToBounds = true
}

func setUnSelectedUI(){
    // reset to default ui
}

希望这会有所帮助!

这篇关于滚动时选择了多个单元格[重复使用单元格问题]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆