CollectionView每隔3秒swift 2自动突出显示下一个单元格

时间:2016-10-19 09:00:53

标签: ios swift2 uicollectionviewcell

我正在尝试创建像BigBasket.com这样的水平滑块。我正在使用collectionview进行分页。我成功地水平,自动和手动滚动项目。但是我没有得到如何每3秒一个接一个地突出下一个细胞。请帮我这样做。

override func viewDidAppear(animated: Bool) {
    rowToHighlight = 0;
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(BannerTagCollectionViewController.ScrollToNextCell), userInfo: nil, repeats: true)
}

func ScrollToNextCell() {
    rowToHighlight += 1
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(BannerTagCollectionViewController.ScrollToNextCell), userInfo: nil, repeats: true)
}

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return bannerTagArray.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : BannerTagCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! BannerTagCollectionViewCell

    if(indexPath.row == self.rowToHighlight){
        cell.backgroundColor = UIColor .whiteColor()
        cell.selectedView.backgroundColor = UIColor .greenColor()
    }
    else{
        cell.backgroundColor = UIColor .lightGrayColor().colorWithAlphaComponent(0.5)
        cell.selectedView.backgroundColor = UIColor .clearColor()
    }

    // Configure the cell
    cell.bannerTagProNameLbl?.text = bannerTagArray[indexPath.row]
    cell.bannerTagProDescLbl?.text="bbbbnn"
    return cell
}

1 个答案:

答案 0 :(得分:0)

那么,你想要的是在3秒后逐个突出显示一个单元格?

这是一个可能有用的代码的和平。

SampleController.h

@interface SampleController

//The other properties and methods

@property int rowToHighlight;

@end


SampleController.m

// your all code


-(void)viewDidAppear:(BOOL)animated{
     //your all other code
     self.rowToHighlight = 0;
     [self performSelector:@selector(callMe) withObject:nil afterDelay:3.0];
}

-(void)callMe{
    [self.CollectionView reloadData];
    self.rowToHighlight += 1;
    [self performSelector:@selector(callMe) withObject:nil afterDelay:3.0];   
}

//All your delegates of collectionView

- (UICollectionView *)collectionView:(UICollectionView *)tableView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell cell = //your code

  //cell operation

  if(indexPath.row == self.rowToHighlight){
        cell.backgroundColor = [UIColor redColor];
  }
  else{
        cell.backgroundColor = [UIColor clearColor];
  }
}


//Your other code

如果有这项工作,请告诉我们。