在重新排序单元格后修复Collectionview单元格之间的间隙

时间:2019-08-12 09:03:58

标签: swift collectionview

我正在使用CollectionView进行单元格重新排序,但是我在重新排序单元格时遇到了问题,而不是在单元格之间创建间隙,而在重新排序单元格后如何解决单元格之间的间隙。 我正在使用三个CollectionView和两个CollectionView单元格宽度是动态的。

extension SentenceBuilderTableViewController {


func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
    if collectionView == tagCollectioinView {
        return true
    }else {
        return false
    }
}

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    if collectionView == tagCollectioinView {
        let item = str_arr.remove(at: sourceIndexPath.item)
        str_arr.insert(item, at: destinationIndexPath.item)

        textStr = str_arr.joined(separator: " ")
        tagCollectioinView.reloadData()

        print(str_arr)
    }
    else {

    }
}

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

    if collectionView == suggectionCollectionView {
        return 1
    }
    else {
        return categoryArr.count
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if collectionView == tagCollectioinView {
        return str_arr.count
    }
    if collectionView == suggectionCollectionView {
        return suggestionArr.count
    }
    else {

        let catIdict = categoryArr[collectionView.tag] as? NSDictionary
        if catIdict != nil {

            let id = catIdict?["categoryID"] as! Int
            if section == id {

                print(textByCategoryDict["\(id)"] ?? "")
                let countArr = textByCategoryDict["\(id)"] as? NSArray
                print(countArr?.count  ??  0)
                return countArr?.count ?? 0
            }

            else {
                return 0
            }
        }
        else {
            return 0
        }
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == tagCollectioinView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TagCollectionCell", for: indexPath as IndexPath) as! TagCollectionCell

        cell.tagNameLbl.text  = str_arr[indexPath.row]
        cell.backgroundColor = UIColor.cyan // make cell
        cell.tagRemoveBtn.tag = indexPath.row
        cell.tagRemoveBtn.addTarget(self, action: #selector(deleteUser), for: UIControl.Event.touchUpInside) //Add target for delete tag
        cell.layer.cornerRadius = 10
        cell.clipsToBounds = true
        return cell

    }
    else if collectionView == suggectionCollectionView {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SuggestionCell", for: indexPath as IndexPath) as! SuggestionCell

        cell.lblName.layer.cornerRadius = 10
        cell.lblName.text = suggestionArr[indexPath.row] as? String
        cell.clipsToBounds = true
        return cell
    }

    else {

        guard let cell: IndexedCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: IndexedCollectionViewCell.identifier, for: indexPath) as? IndexedCollectionViewCell else {
            fatalError("UICollectionViewCell must be of IndexedCollectionViewCell type")
        }

        guard let indexedCollectionView: IndexedCollectionView = collectionView as? IndexedCollectionView else {
            fatalError("UICollectionView must be of IndexedCollectionView type")
        }

        cell.layer.cornerRadius = 10
        cell.clipsToBounds = true
        let catIdict = categoryArr[indexedCollectionView.indexPath.section] as? NSDictionary
        let id = catIdict?["categoryID"] as! Int
        let countArr = textByCategoryDict["\(id)"] as? NSArray
        cell.setLabelText(countArr![indexPath.row ] as! String)
        print(countArr?.count)
        cell.backgroundColor = colorsDict[indexedCollectionView.indexPath.section]?[indexPath.row]

        return cell
    }

}

// MARK: <UICollectionViewDelegate Flow Layout>

func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, insetForSectionAt _: Int) -> UIEdgeInsets {

    if  collectionView == tagCollectioinView {
        return UIEdgeInsets(top:5, left: 5 , bottom:5, right: 5)

    }

    else if collectionView == suggectionCollectionView {
        return UIEdgeInsets(top:5, left: 5 , bottom:5, right: 5)
    }

    else {
        return UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
    }
}

func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt  indexPath: IndexPath) -> CGSize {

    if collectionView == tagCollectioinView {

        let label = UILabel(frame: CGRect.zero)
        label.sizeToFit()

        let size = ( str_arr[indexPath.row] as NSString).size(withAttributes: nil)
        // return CGSize(width: 120, height: 35)
        let tableViewCellHeight: CGFloat = tableViewRowHeight//tableView.rowHeight
        let collectionItemWidth: CGFloat = tableViewCellHeight - (collectionLeftInset + collectionRightInset)
        let collectionViewHeight: CGFloat = collectionItemWidth

        print(self.tagCollectioinView.frame.width)
        let tagLabelSize = collectionItemWidth + size.width + 20

        if tagLabelSize > self.tagCollectioinView.frame.width {

               return CGSize(width: 370  , height: collectionViewHeight - 40 )
        }
        else {
               return CGSize(width: tagLabelSize , height: collectionViewHeight - 40 )
        }

    }

    else if collectionView == suggectionCollectionView {

        let label = UILabel(frame: CGRect.zero)
        label.sizeToFit()
        let size = ( suggestionArr[indexPath.row] as! NSString).size(withAttributes: nil)
        let tableViewCellHeight: CGFloat = tableViewRowHeight//tableView.rowHeight
        let collectionItemWidth: CGFloat = tableViewCellHeight - (collectionLeftInset + collectionRightInset)
        let collectionViewHeight: CGFloat = collectionItemWidth


        return CGSize(width: collectionItemWidth + 20 , height: 50 )        }

    else {

        let label = UILabel(frame: CGRect.zero)
        guard let indexedCollectionView: IndexedCollectionView = collectionView as? IndexedCollectionView else {
            fatalError("UICollectionView must be of IndexedCollectionView type")
        }

        let catIdict = categoryArr[indexedCollectionView.indexPath.section] as? NSDictionary
        let id = catIdict?["categoryID"] as! Int
        let countArr = textByCategoryDict["\(id)"] as? NSArray
        label.text =   countArr![indexPath.row ] as? String
        label.sizeToFit()
        let size = ( label.text)?.size(withAttributes: nil)

        let tableViewCellHeight: CGFloat = tableViewRowHeight//tableView.rowHeight
        let collectionItemWidth: CGFloat = tableViewCellHeight - (collectionLeftInset + collectionRightInset)

        let collectionViewHeight: CGFloat = collectionItemWidth
        return CGSize(width: collectionItemWidth + size!.width + 40 , height: collectionViewHeight - 30 )
    }

}

func collectionView(_ collection: UICollectionView, layout _: UICollectionViewLayout, minimumLineSpacingForSectionAt _: Int) -> CGFloat {

    if collection == tagCollectioinView {

        return 5
    }
    else {

    return 5
    }

}




func collectionView(_ collection: UICollectionView, layout _: UICollectionViewLayout, minimumInteritemSpacingForSectionAt _: Int) -> CGFloat {

    if collection == tagCollectioinView {

        return 5
    }

    return 5
}

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

    if collectionView == tagCollectioinView {

    }
    else if collectionView == suggectionCollectionView {
        let str =  suggestionArr[indexPath.row]
        str_arr.append(str as! String)
        suggestNextSentance(word: str as! String ) //Calling Suggesstion function
        textStr = str_arr.joined(separator: " ")
        tagCollectioinView.reloadData()
    }

    else {

        guard let indexedCollectionView: IndexedCollectionView = collectionView as? IndexedCollectionView else {
            fatalError("UICollectionView must be of IndexedCollectionView type")
        }

        let catIdict = categoryArr[collectionView.tag] as AnyObject
        print(catIdict)
        let id = catIdict["categoryID"] as! Int
        let countArr = textByCategoryDict["\(id)"] as! NSArray
        print(indexPath.row)
        let word =  countArr[indexPath.row]
        print(word)

        //Calling Suggesstion function
        suggestNextSentance(word: word as? String ?? "")

        str_arr.append(word as! String)
        textStr = str_arr.joined(separator: " ")
        tagCollectioinView.reloadData()
    }
}

0 个答案:

没有答案