2016-10-19 5 views
0

BigBasket.comのような水平スライダを作成しようとしています。私はページングのためにcollectionviewを使用しています。私は項目を水平、自動、手動の両方でスクロールすることに成功しています。 3秒ごとに次のセルを1つずつ強調表示する方法がわかりません。私がこれを行うのを手伝ってください。CollectionViewは3秒ごとに自動的に次のセルにハイライト2

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 
} 

答えて

0

だから、3秒後に1つずつセルを強調表示したいですか?

ここには、参考になると思われるコードがあります。

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 

この場合はお知らせください。

+0

すぐにコードを変換して試しました。しかし、それは動作していません。最初のセルだけが強調表示されています。私はコードをアップロードしました。 – user3516627

+0

@ user3516627の場合は、 * ScrollToNextCell *メソッドに* self.collectionView!.reloadData()*を追加するのを忘れてしまいました。 – PrafulD

+0

ええ。それは作品です。ありがとう。 – user3516627

関連する問題