在视图控制器ViewController中如何实现数据源协议与委托协议?

       集合视图的委托协议是UICollectionViewDelegate,数据源协议是UICollectionViewDataSource。 

       UICollectionViewDataSource中的方法有如下4个。
       collectionView:numberOfItemsInSection:。提供某个节中的列数目。
       numberOfSectionsInCollectionView:。提供视图中节的个数。
       collectionView:cellForItemAtIndexPath:。为某个单元格提供显示数据。
       collectionView:viewForSupplementaryElementOfKind:atIndexPath:。为补充视图提供显示数据。 

       下面南昌APP开发公司-易速网络就来为大家介绍一下在视图控制器ViewController中实现数据源协议UICollectionViewDataSource和委托协议UICollectionViewDelegate的方法。

       在视图控制器ViewController中实现数据源协议UICollectionViewDataSource的代码如下: 

       override func numberOfSectionsInCollectionView(collectionView:     UICollectionView) -> Int {     

       return self.events.count / 2 

       }  

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

       return 2 

       }  

       override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {    

       var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as Cell 

       let event = self.events[indexPath.section*2 + indexPath.row] as NSDictionary    cell.label.text = event["name"] as? String     

       let imageFile = event["image"] as String     

       cell.imageView.image = UIImage(named: imageFile)     

       return cell 

       } 

       - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView     *)collectionView 

       {     

       return [self.events count] / 2; 

       } 

       - (NSInteger)collectionView:(UICollectionView *)collectionView      numberOfItemsInSection:(NSInteger)section 

       {     

       return 2; 

       } 

       - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView     cellForItemAtIndexPath:(NSIndexPath *)indexPath 

       {     

       Cell *cell = [collectionView         

       dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

       NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];     

       cell.label.text = [event objectForKey:@"name"];     

       cell.imageView.image = [UIImage imageNamed:[event objectForKey:@"image"]];    return cell; 

       } 

       UICollectionViewDelegate中的方法很多,这里我们选择几个较为重要的加以介绍。 

       collectionView:didSelectItemAtIndexPath:。选择单元格之后触发。 

       collectionView:didDeselectItemAtIndexPath:。不选择单元格之后触发。 

       在视图控制器ViewController中实现委托协议UICollectionViewDelegate的代码如下: 

       override func collectionView(collectionView: UICollectionView,      didSelectItemAtIndexPath indexPath: NSIndexPath) {     

       let event = self.events[indexPath.section*2 + indexPath.row] as NSDictionary    NSLog("select event name : %@", event["name"] as String) 

       }

       - (void)collectionView:(UICollectionView *)collectionView      

       didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

      {     

       NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];     

       NSLog(@"select event name : %@", [event objectForKey:@"name"]);  

       } 

       运行上述代码,得到的输出结果如下: 

       select event name : basketball 

       select event name : athletics 

       select event name : archery

       关于在视图控制器ViewController中实现数据源协议与委托协议的方法就先介绍到这里,如果还有哪些地方不太懂的,欢迎来电和我们联系。此外,易速网络专业为您提供南昌APP开发,小程序开发,网站建设等服务,如有需要,欢迎大家来电咨询,洽谈合作!

提供全面的搜索引擎优化学习交流,专注网站优化和搜索引擎营销推广服务。用专业的SEO技术为企业网站提升关键词排名,让你的网站不仅满足用户体验还要适合搜索引擎优化规则。
易速网站优化公司 » 在视图控制器ViewController中如何实现数据源协议与委托协议?
享更多特权,立即登录下载海量资源
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡