iOS学习笔记-038.UITableView示例二——九宫格

UITableView示例二——九宫格.

一、代码

1.XmCell.h

//
//  XmCell.h
//  03_UIView_31_九宫格
//
//  Created by 杞文明 on 2016/01/13 23:48:12   星期三
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import <UIKit/UIKit.h>
//这个参数是用来计算  我们设置的 tag的开始值,为了和 UITableViewCell 的tag区分
#define kStartTag 1000
//每一个行的按钮数
#define kCountPerButton 4
//总得按钮数
#define kTotalButton 50
@interface XmCell : UITableViewCell
@end

2.XmCell.m

//
//  XmCell.m
//  03_UIView_31_九宫格
//
//  Created by 杞文明 on 2016/01/13 23:48:22   星期三
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "XmCell.h"

@implementation XmCell

#pragma 构造方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //循环创建一行
        for (NSInteger i=0; i<kCountPerButton; i++) {
            //1.计算按钮的宽度
            NSInteger butonWidth = 375.0/kCountPerButton;
            //2.实例化按钮
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            //3.设置按钮的大小
            [button setFrame:CGRectMake(i*butonWidth, 0, butonWidth, 100)];
            [button setTag:i+kStartTag ];
            //4.添加到contentView中
            [self.contentView addSubview:button];
        }
    }
    return self;
}

- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
@end

3.ViewController.m

//
//  ViewController.m
//  03_UIView_31_九宫格
//
//  Created by 杞文明 on 2016/01/13 23:48:34   星期三.
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "ViewController.h"
#import "XmCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //实例化 UITableView
    UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [self.view addSubview:tableView];

    //让表格注册“自定义单元格类”
    [tableView registerClass:[XmCell class] forCellReuseIdentifier:@"XmCell"];
}

#pragma mark - 行数
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //计算函数的方法,就是总的个数/每行的个数+1, 这里面只有一个分组
    NSLog(@"行数:%d",(kTotalButton-1)/kCountPerButton +1);
    return (kTotalButton-1)/kCountPerButton +1;
}

#pragma mark - 单元格
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    static NSString * identifier = @"XmCell";
    XmCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    for (NSInteger i=0; i<kCountPerButton; i++) {
        //1.获取图片的地址
        NSString * path = [NSString stringWithFormat:@"tmall_icon_cat_outing_%d.png",arc4random_uniform(12)+1];
        //2.生成图片
        UIImage * image = [UIImage imageNamed:path];
        //3.获取按钮
        UIButton * button = (UIButton*)[cell.contentView viewWithTag:i+kStartTag];
        //4.设置按钮图片
        [button setImage:image forState:UIControlStateNormal];
        // 如果单元行是从缓存池中提取的,那么如果接近末尾时,最后的按钮会被显示出来
        //比如 我们有50个按钮,每行3个,最有一行,如果不加下面的判断会出现3个,二事实上是2个
        // 针对这一问题,需要把超出的按钮隐藏起来
        if (kCountPerButton * indexPath.row + i < kTotalButton ) {
            [button setHidden:NO];
        }else{
            [button setHidden:YES];
        }
    }
    return cell;
}

#pragma mark - 行高
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 100;
}
@end

二、图示

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值