美文网首页demo
iOS电商项目之分类

iOS电商项目之分类

作者: 葱花饼 | 来源:发表于2017-06-12 09:11 被阅读269次
    �分类.PNG

    先创建左边的tableView和右边的collectionView

    UITableView *tableView = [[UITableView alloc]init];
        self.tableView = tableView;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = ControllerBgColor;
        tableView.frame = CGRectMake(0, 64, ScreenWidth / 4, ScreenHeight);
        tableView.showsVerticalScrollIndicator = NO;
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        tableView.tableFooterView = [[UIView alloc]init];
        [self.view addSubview:tableView];
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CategoryCell class]) bundle:nil] forCellReuseIdentifier:@"CategoryCell"];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0.5;
        layout.minimumInteritemSpacing = 0.5;
        CGFloat w = (ScreenWidth - ScreenWidth / 4) / 3 - 0.5;
        CGFloat h = 44;
        layout.itemSize = CGSizeMake(w, h);
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        self.collectionView = collectionView;
        collectionView.frame = CGRectMake(tableView.frame.size.width, 64, (ScreenWidth - ScreenWidth / 4), ScreenHeight);
        collectionView.backgroundColor = ControllerBgColor;
        [collectionView registerClass:[UserCell class] forCellWithReuseIdentifier:@"UserCell"];
        [self.view addSubview:collectionView];
    

    点击tableViewCell更新collectionView

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CategoryModel_result *result = self.categoryModel.result[indexPath.row];
        self.collectionCellArray = result.children;
        [self.collectionView reloadData];
    }
    

    collectionView代理实现

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        if (self.collectionCellArray == 0) {
            CategoryModel_result *result = self.categoryModel.result[section];
            return result.children.count;
        } else {
            return self.collectionCellArray.count;
        }
    }
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UserCell" forIndexPath:indexPath];
        if (self.collectionCellArray == 0) {
            CategoryModel_result *result = self.categoryModel.result[indexPath.section];
            CategoryModel_result_children *children = result.children[indexPath.row];
            cell.label.text = children.name;
        } else {
            CategoryModel_result_children *children = self.collectionCellArray[indexPath.row];
            cell.label.text = children.name;
        }
        return cell;
    }
    

    想要系统,具体的学习,请加群-------群号:647657839

    相关文章

      网友评论

        本文标题:iOS电商项目之分类

        本文链接:https://www.haomeiwen.com/subject/amakqxtx.html