UICollectionView不重用Cell

时间:2015-05-18 07:34:33

标签: ios swift

我的UIViewController中有一个UICollectionview。滚动集合视图不重用单元格。您可以看到 numberOfItemsInSection 方法;

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

      return 7

    }

这是 collectionViewLayout 方法;

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        if(collectionView == mainCollectionView){
            switch(indexPath.row){
            case 0:
                return CGSize(width: self.view.frame.width, height: self.view.frame.height * 0.20)
            case 1:
                return CGSize(width: (self.view.frame.width * 0.5) - 3, height: self.view.frame.height * 0.2)
            case 2:
                return CGSize(width: (self.view.frame.width * 0.5), height: self.view.frame.height * 0.2)
            case 3:
                return CGSize(width: self.view.frame.width, height: self.view.frame.height * 0.2)
            case 4:
                return CGSize(width: (self.view.frame.width * 0.6) - 3, height: self.view.frame.height * 0.2)
            case 5:
                return CGSize(width: (self.view.frame.width * 0.4), height: self.view.frame.height * 0.2)
            case 6:
                return CGSize(width: (self.view.frame.width), height: self.view.frame.height * 0.2)
            default:
                return CGSize(width: 0, height: 0)
            }
        }
    }

最后一个是 cellForRowAtIndexPath 方法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if(collectionView == mainCollectionView){
            var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell

            switch(indexPath.row){
            case 0:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 1:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 2:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = false
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 3:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
            case 4:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 5:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = false
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            case 6:
                cell.imageView.image = UIImage(named: imageArray[indexPath.row])
                cell.clockIconImageView.hidden = true
                cell.pinIconImageView.hidden = true
                cell.placeLabel.hidden = true
                cell.lineView.hidden = false
            default:
                break
            }
            return cell
        }

你也可以看到输出:

CollectionView Image

我在 case6 僵尸程序中添加了一个断点,它没有输入该情况。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:3)

我认为处理每个案例都不是一个好主意。

您应该创建一个包含单元格参数的包装器对象。

在您的情况下,它应该类似于以下内容:

          <span>Book now</a> <!--<div class="disp"><p>-->

          <div id="light" class="white_content"><label>Email:</label><?=$vehicle->Email; ?> </p><p><label>Mobile:</label><?=$vehicle->Mobile; ?></p>
          <p><form id="form_<?php echo $vid;?>" name="form1" method="post" action="">
          <?php //echo $_REQUEST['email'];?>

                  <p>
                   <input type="hidden" name="vehicleid" value="<?php echo $vid;?>" id="vehicleid" />
                    <label>
                      <input type="radio" name="RadioGroup1_<?php echo $vid;?> " value="1" id="bstatus<?php echo $vid;?>" onClick="idForm(<?php echo $vid;?>)" />
                      Conform</label>
                    <br />
                    <label>
                      <input type="radio" name="RadioGroup1<?php echo $vid;?>" value="0" id="bstatus1<?php echo $vid;?>" onClick="idForm(<?php echo $vid;?>)" />
                      Not Conform</label>
                    <br />
                  </p>

                </form><a href = "javascript:void()" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"><b>X</b></a></div>
<div id="fade" class="black_overlay"></div>

创建这些对象的数组(例如,在class YourWrapperObject { var image: UIImage var clockIconHidden: Bool var size: CGSize // Your other vars } 方法中),然后在viewDidLoad方法中:

cellForRowAtIndexPath

以及func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell = /*get your cell using reusable identifier*/ let cellParameters = yourObjectArray[indexPath.row] /* Then assign each parameters */ cell.imageView.image = cellParameters.image cell.clockIconImageView.hidden = cellParameters.clockIconHidden cell.pinIconImageView.hidden = cellParameters.pinIconHidden cell.placeLabel.hidden = cellParameters.placeHidden cell.lineView.hidden = cellParameters.lineHidden /* return your cell */ return cell } 方法:

sizeForItemAtIndexPath

您的代码将更具可读性和灵活性

答案 1 :(得分:0)

在主线程中重新加载UICollectionView数据解决了我的问题。我用过:

dispatch_async(dispatch_get_main_queue()) {
                    mainCollectionView.reloadData()
                }