百恒网络
在视图控制器ViewController中如何实现数据源协议与委托协议?
  • 百恒服务
  • APP开发
  • 网页案例
  • 网页知识
  • 关于我们
  • 联系我们
  • 在视图控制器ViewController中如何实现数据源协议与委托协议?

    2017-11-16 15:36:05 3255
           集合视图的委托协议是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开发,小程序开发,网站建设等服务,如有需要,欢迎大家来电咨询,洽谈合作!
    展开分享
    服务
    案例
    首页
    动态
    联系
    咨询