迅速:collectionView部分

时间:2018-08-04 07:49:00

标签: ios swift

我在collectionView中有7个部分。我想在所有部分中显示图像,除了第一个和最后一个。

我使用以下代码进行操作:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    if sectionNumber == 0 {
        //hide image
    } else {
        //show image
    }

    if sectionNumber == 6 {
        //hide image
    } else {
        //show image
    }

}

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

        sectionNumber = indexPath.section
        print("indexPath.section\(indexPath.section)")
}

但是代码不起作用。当我滚动到最后一部分时,我的图像隐藏在6部分(代码中为5,因为第一部分== 0)。如果我滚动到第一部分,我的图像将不会隐藏。

我还有第二种代码变体。在此变体中,一切正常,除非我滚动到第一部分,然后我的图像隐藏在第二部分。

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let currentlyVisibleSection = collectionView.indexPathsForVisibleItems.map { $0.section }.min()!

        if currentlyVisibleSection < 5 {
            let scrollToSection = currentlyVisibleSection + 1
            //show image
        }

        if currentlyVisibleSection == 5 {
            let scrollToSection = currentlyVisibleSection + 1
            //hide image
        }

        if currentlyVisibleSection != 1 {
            let scrollToSection = currentlyVisibleSection - 1
            //show image
        }

        if currentlyVisibleSection == 1 {
            let scrollToSection = currentlyVisibleSection - 1
            //hide image
        }
}

更新

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

        if indexPath.section == 0 {
            print("firstSection")
        }

        if (indexPath.section + 1) == dataModel.count {
            print("lastSection")
        }
}

3 个答案:

答案 0 :(得分:2)

我不确定spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { View child1 = getLayoutInflater().inflate(R.layout.part_bookdetails, null); View child2 = getLayoutInflater().inflate(R.layout.part_bookdetails, null); View child3 = getLayoutInflater().inflate(R.layout.part_bookdetails, null); switch (position){ case 0: frameLayout1.removeAllViews(); frameLayout2.removeAllViews(); frameLayout3.removeAllViews(); frameLayout1.addView(child1); break; case 1: frameLayout1.removeAllViews(); frameLayout2.removeAllViews(); frameLayout3.removeAllViews(); frameLayout1.addView(child1); frameLayout2.addView(child2); break; case 2: frameLayout1.removeAllViews(); frameLayout2.removeAllViews(); frameLayout3.removeAllViews(); frameLayout1.addView(child1); frameLayout2.addView(child2); frameLayout3.addView(child3); break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //hide image后面隐藏着什么,但是为什么不将逻辑移到单元格本身呢?每次显示该单元格时,它都会重复使用旧的单元格,并且会调用//show image。因此,您可以在那里或在单元格中确定要显示的行。

因此,您可以创建一个简单的函数来确定该行是第一行还是最后一行。

cellForItemAt

func displayImage(on indexPath: IndexPath) -> Bool { if indexPath.section == 0 { return false } if (indexPath.section + 1) == dataModel.count { return false } return true } 必须是两级数组

dataModel

每个子数组代表一个部分。

答案 1 :(得分:1)

scrollViewDidEndDecelarating之后,您是否尝试过reloadData()?

我尝试提出另一种方法:

 override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

            guard let cell = cell as? CollectionViewCell else {return}

            switch indexPath.section {
            case 0,6: //in assumption number of sections = 7
                cell.myImage.isHidden = true
            default:
                cell.myImage.isHidden = false
            }
        }

答案 2 :(得分:0)

> REMOVE:- 'scrollViewDidEndDecelerating' methods

    let dataModel = [["item","item","item"],["item","item","item"],["item","item","item"],["item","item","item"],["item","item","item"],["item","item","item"],["item","item","item"]]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        let sectinData = dataModel[section]as! NSArray
        return sectinData.count - 1
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataModel.count - 1
    }

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

            if indexPath.section == 0  || indexPath.section == dataModel.count - 1 {
                print(“hide image“)
            }else{
    print(“show image“)
        }
    }