• UITableview和 UICollectionView的区别和用法: (xib定制cell)


    UITableview 和 UICollectionView 在ios开发中运用的十分广泛:接下介绍一下各个的用法:(xib的使用方法:)

    一.UItableview 的使用代理方法:<UITableviewDatasource ,UITableDelegate>
      1.首先定制好cell在xib上面,和拉取需要的属性:如何所示

    在cell.h中拉取属性,全部暴露出来:

    #import <UIKit/UIKit.h>

    @interface JingpinTableViewCell : UITableViewCell

    @property (weak, nonatomic) IBOutlet UIImageView *iconImageview;

    @property (weak, nonatomic) IBOutlet UILabel *ProductTitleLabel;

    @property (weak, nonatomic) IBOutlet UILabel *ProductDetailLabel;

    @property (weak, nonatomic) IBOutlet UILabel *ProductLabel;

    @end

    在cell.m中不写任何属性:

    #import "JingpinTableViewCell.h"

     @implementation JingpinTableViewCell

     - (void)awakeFromNib {

        // Initialization code

    }

     - (void)setSelected:(BOOL)selected animated:(BOOL)animated {

        [super setSelected:selected animated:animated];

         // Configure the view for the selected state

    }

    @end

    2.在controllerview.xib里面拉取tableview,拉取属性:
    3.在controllerview.h实现文件

    #import <UIKit/UIKit.h>

    #import "MyViewController.h"

    @interface JingpinViewController : MyViewController

    @end

    4.controllerview.m的实现文件:

    #define JINGPINCELL @"JingPinCell"

    #import "JingpinViewController.h"

    #import "JingpinTableViewCell.h"

    #import "UIImageView+WebCache.h"

    #import "Goods.h"

    #import "MyDBHelper.h"

    #import "MBProgressHUD+MJ.h"

    #import "ShopCarViewController.h"

    @interface JingpinViewController()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

    @property (weak, nonatomic) IBOutlet UITableView *homeTableView;

    @property (nonatomic,strong)NSArray *dataArray;

    @end

    @implementation JingpinViewController

     - (void)viewDidLoad {

        [super viewDidLoad];

        [self.homeTableView registerNib:[UINib nibWithNibName:@"JingpinTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:JINGPINCELL ];

        self.dataArray = [[NSArray alloc]init];

    }

    #pragma mark UITableViewDelagate 

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return self.dataArray.count;

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        JingpinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:JINGPINCELL forIndexPath:indexPath];

    //模型开发

        int row = (int)indexPath.row;

        Goods *goods = self.dataArray[row];

        [cell.iconImageview sd_setImageWithURL:[NSURL URLWithString:goods.imageUrl]placeholderImage:nil];//加载图片

        cell.ProductTitleLabel.text = goods.name;

        cell.ProductDetailLabel.text = goods.info;

        cell.ProductLabel.text = [NSString stringWithFormat:@"%.2f",goods.price];

        return cell;

    }

     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 80;

    }

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        int row = (int)indexPath.row;

        UIAlertView *alterview = [[UIAlertView alloc]initWithTitle:@"请选择操作" message:nil delegate :self cancelButtonTitle:@"取消" otherButtonTitles:@"立即购买",@"加入购物车", nil];

        alterview.tag = row;

        [alterview show];

    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

      {

          int row = (int)alertView.tag;

          Goods *g = self.dataArray[row];

          Goods *good  = [[Goods alloc]init];

          good.goodsId = g.goodsId;

          good.count  = 1;

        if (buttonIndex == 1) {

            NSLog(@"立即购买");

            ShopCarViewController *shopCarVC = [[ShopCarViewController alloc]init];

            shopCarVC.arry_gvc = self.dataArray;

            shopCarVC.fromCart = NO;

            shopCarVC.liji_arry = [[NSMutableArray alloc]initWithObjects:good , nil ];

            [self presentViewController:shopCarVC animated:NO completion:nil];

        }

        else if (buttonIndex == 2){

            NSLog(@"加入到购物车");

            [[MyDBHelper getInstance]addGoodToCar:good];

            [MBProgressHUD showSuccess:@"添加商品成功,请到购物车里面查看"];

        }

        }

    }

    - (IBAction)backBtn:(UIButton *)sender {

        [self dismissViewControllerAnimated:NO completion:nil];

    }

     - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    效果图:

    二.uicollecview的使用方法:<UIcollectionviewDelegate,UIcollectionDatasouce ,UICollectionDelegateFewlayout>
    1.定制cell在和拉取属性:
    cell.h的实现文件:

    #import <UIKit/UIKit.h>

    @interface FenleiCollectionViewCell : UICollectionViewCell

    @property (weak, nonatomic) IBOutlet UIImageView *iconImageview;

    @property (weak, nonatomic) IBOutlet UILabel *titleLabel;

    @end

    cell.m的实现文件:

    #import "FenleiCollectionViewCell.h"

     @implementation FenleiCollectionViewCell

     - (void)awakeFromNib {

    }

     @end

    FenleiCollectionReusableView.h文件 (collection的头部view)

    #import <UIKit/UIKit.h>

    @interface FenleiCollectionReusableView : UICollectionReusableView

    @property (weak, nonatomic) IBOutlet UILabel *titlelabe;

    @end

     2.在collecview.xib里面collecview的代理方法;<>

    collecview.m的实现文件:

    #import "FenleiViewController.h"

    #import "FenleiCollectionViewCell.h"

    #import "UIImageView+WebCache.h"

    #import "Goods.h"

    #import "MyDBHelper.h"

    #import "ShopCarViewController.h"

    #import "MBProgressHUD+MJ.h"

    #import "FenleiCollectionReusableView.h"

    @interface FenleiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIAlertViewDelegate,UICollectionViewDelegateFlowLayout>

    @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

    @property (nonatomic,strong)NSArray *array;

    @property (nonatomic,strong)NSMutableArray *array1, *array2 ,*array3,*array4,*array5;

    @end

    @implementation FenleiViewController

     - (void)viewDidLoad {

        [super viewDidLoad];

        self.array =  [[NSArray array]init];

        self.array1 = [[NSMutableArray alloc]init];

        self.array2 = [[NSMutableArray alloc]init];

        self.array3 = [[NSMutableArray alloc]init];

        self.array4 = [[NSMutableArray alloc]init];

        self.array5 = [[NSMutableArray alloc]init];

        self.collectionView.delegate = self;

        self.collectionView.dataSource = self;

        //注册头部

        [self.collectionView registerNib:[UINib nibWithNibName:@"FenleiCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FenleiCollectionReusableView"];

        [self.collectionView registerNib:[UINib nibWithNibName:@"FenleiCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FenleiCollectionViewCell"];

       ////    tap手势

    //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touch:)];

    //    [self.collectionView addGestureRecognizer:tap];  

    }

     -(void)touch:(UITapGestureRecognizer *)gesture{

         CGPoint point  =  [gesture locationInView:self.collectionView];

        if (gesture.state == UIGestureRecognizerStateBegan) {

            NSLog(@"tap");

            NSIndexPath *indexpath = [self.collectionView indexPathForItemAtPoint:point];

            if (indexpath == nil) {

                 }

            else{

                UIAlertView *alterview = [[UIAlertView alloc]initWithTitle:@"请选择操作" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即购买",@"加入购物车", nil];

                alterview.tag =  indexpath.row;

                [alterview show];

             }

        } 

    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

        int row = (int)alertView.tag;

        Goods *good = self.array[row];

        if (buttonIndex == 1) {

            //立即购买

            ShopCarViewController *shopVC = [[ShopCarViewController

                                        alloc]init];

            shopVC.fromCart = NO;

            shopVC.arry_gvc = self.array;

            shopVC.liji_arry = [[NSMutableArray alloc]initWithObjects:good, nil];

            [self presentViewController:shopVC animated:NO completion:nil];

        }

        if (buttonIndex == 2) {

            //加入购物车

            [[MyDBHelper getInstance]addGoodToCar:good];

            [MBProgressHUD showSuccess:@"成功加入到购物车"];

        }

        

    }

     -(void)setData:(NSArray *)data{

        self.array = data;

        for (Goods *g in self.array) {

           NSString  *type  = g.type;

            if ([type isEqualToString:@"智能照明"]) {

                [self.array1 addObject:g];

            }

            else if ([type isEqualToString:@"智能电工"]){

                [self.array2 addObject:g];

            }

            else if ([type isEqualToString:@"智能家电"]){

               [self.array3 addObject:g];

            }

            else if ([type isEqualToString:@"智能传感"]){

                [self.array4 addObject:g];

        

            }

            else if ([type isEqualToString:@"智能遥控"]){

            

                [self.array5 addObject:g];

            }

        }

        [self.collectionView reloadData];

    }

    #pragma mark - UICollection

    //分成几个区默认为1

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

        return 5;

     }

    //返回的数据的多少

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

        

        switch ( section) {

            case 0:

                return self.array1.count;

                break;

            case 1:

                return self.array2.count;

                break;

            case 2:

                return self.array3.count;

                break;

            case 3:

                return self.array4.count;

                break;

            case 4:

                return self.array5.count;

                break;

            default:

              break;

        }

        return 0;

    }

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

    {

        FenleiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FenleiCollectionViewCell" forIndexPath:indexPath];

        int section= (int)indexPath.section;

        int row = (int)indexPath.row;

        Goods *goods;

        switch (section) {

            case 0:

                goods = self.array1[row];

                break;

            case 1:

                goods = self.array2[row];

                break;

            case 2:

                goods = self.array3[row];

                break;

            case 3:

                goods = self.array4[row];

                break;

            case 4:

                goods = self.array5[row];

                break;

            default:

                break;

        }

        [cell.iconImageview sd_setImageWithURL:[NSURL URLWithString:goods.imageUrl]];

        cell.titleLabel.text = goods.name;

        return cell;

        

    }

     //上面图标的方法

    -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

        FenleiCollectionReusableView *view =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FenleiCollectionReusableView" forIndexPath:indexPath];

        int row = (int)indexPath.section;

        switch (row) {

            case 0:

                view.titlelabe.text = @"智能照明";

                break;

            case 1:

                view.titlelabe.text = @"智能家电";

                break;

            case 2:

                view.titlelabe.text = @"智能电工";

                break;

            case 3:

                view.titlelabe.text = @"智能传感";

                break;

            case 4:

                view.titlelabe.text = @"智能遥控器";

                break;

            

            default:

                break;

        }

        return view;

    }

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 

    }

    #pragma  mark - UICollectionViewDelegateFlowlayout(流布局)

    // 返回item的大小

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

    {

        return CGSizeMake(70, 70);

    }

    //返回是上下左右的宽度

    //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    //    return UIEdgeInsetsMake(10, 10, 0, 10);

    //

    //}

    //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    //    

    //    return 0;

    //}

    ////item的每个之间的间隙

    //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    //   

    //    return 0;

    //

    //}

    ////头部的距离

    //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    //    

    //    

    //    return CGSizeMake(0, 0);

    //}

    //// 尾部的距离

    //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

    // 

    //    return CGSizeMake(0, 10);

    //

    //}

    - (IBAction)backBtn:(UIButton *)sender {

        

        [self dismissViewControllerAnimated:NO completion:nil];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    最后的效果图:

     

     
  • 相关阅读:
    [考试反思]0421省选模拟76:学傻
    [考试反思]0420省选模拟75:安在
    [考试反思]0418省选模拟74:杂枝
    [考试反思]0417省选模拟73:纠结
    [考试反思]0416省选模拟72:停滞
    [考试反思]0415省选模拟71:限制
    [考试反思]0414省选模拟70:阻塞
    [考试反思]0413省选模拟69:遗弃
    [考试反思]0411省选模拟68:毒瘤
    [考试反思]0410省选模拟67:迷惑
  • 原文地址:https://www.cnblogs.com/zhufeng1994/p/4619092.html
Copyright © 2020-2023  润新知