iOS按钮

作者: 盖世英雄的梦想 | 来源:发表于2018-08-23 15:30 被阅读0次

利用表格套用网格

#import "ViewController.h"@interface ViewController ()@end

static NSString *reuseID = @"cell";

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UITableView *tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    tab.delegate=self;

    tab.dataSource=self;

    [self.view addSubview:tab];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}

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

{

    if (section==0)

    {

        return 2;

    }else if (section==1)

    {

        return 1;

    }

    return 0;

}

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

{

    static NSString *reuse=@"reuse";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];

    if (!cell)

    {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];

    }

    if (indexPath.section==0)

    {

    }else if (indexPath.section==1)

    {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        // 设置单元格的大小

        flowLayout.itemSize = CGSizeMake(60, 80);

        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);

        // 创建网格对象

        UICollectionView *cv = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];

        // 设置代理

        cv.delegate = self;

        cv.dataSource = self;

        cv.backgroundColor = [UIColor whiteColor];

        // 将网格添加到视图上

        [cell addSubview:cv];

        [cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseID];

    }

    return cell;

}

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

{

    return 10;

}

// 设置 cell

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

{

    // 根据可重用标识符查找cell

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];

    // 设置cell内容

    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];

    imgV.image=[UIImage imageNamed:@"touxiang.jpg"];

    [cell.contentView addSubview:imgV];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, 60, 30)];

    label.font = [UIFont systemFontOfSize:20];

    label.textColor = [UIColor blackColor];

    label.textAlignment = NSTextAlignmentCenter;

    label.font = [UIFont systemFontOfSize:15];

    NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];

    label.text = arr[indexPath.row];

    [cell.contentView addSubview:label];

    // 返回 cell

    return cell;

}

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

{

    if (indexPath.section==0)

    {

        return 50;

    }else if (indexPath.section==1)

    {

        return 200;

    }

    return 0;

}

相关文章

网友评论

      本文标题:iOS按钮

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