代码改变世界

uitableview做九宫格

2013-06-23 22:25  甘超波  阅读(1734)  评论(0编辑  收藏  举报

1:创建实体

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end

2:自定义cell和button

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end

#import "MyCell.h"
#import "Shop.h"
#import "MyButton.h"

#define kTagPrefix 10


@implementation MyCell

#pragma mark 监听每一格的点击
- (void)itemClick:(MyButton *)item {
    NSLog(@"点击了-%@", item.titleLabel.text);
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 按钮宽度
        CGFloat btnWidth = self.contentView.bounds.size.width / kColumn;
        
        for (int i = 0; i<kColumn; i++) {
            MyButton *btn = [[[MyButton alloc] init] autorelease];
            btn.tag = kTagPrefix + i;
            [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.frame = CGRectMake(btnWidth * i, 0, btnWidth, kCellHeight);
            //btn.backgroundColor = [UIColor redColor];
            [self.contentView addSubview:btn];
        }
        
    }
   
    
    return self;
}

// 假设shops里面有2个
- (void)setRowShops:(NSArray *)shops {
    int count = shops.count;
    
    for (int i = 0; i<kColumn; i++) {
        MyButton *btn = (MyButton *)[self.contentView viewWithTag:kTagPrefix + i];
        
        // 设置数据
        if (i<count) {
            btn.hidden = NO;
            Shop *shop = [shops objectAtIndex:i];
            // 设置背景上面的小图片
            [btn setImage:[UIImage imageNamed:shop.icon] forState:UIControlStateNormal];
            
            [btn setTitle:shop.name forState:UIControlStateNormal];
        } else {
            btn.hidden = YES;
        }
    }
}
@end

自定义button

#import "MyButton.h"

#define kImageRatio 0.6

#define kMarginRatio 0.1

#define kLabelRatio (1 - kImageRatio - 2 * kMarginRatio)

@implementation MyButton

- (id)init {
    if (self = [super init]) {
        // 设置文字颜色  一定要设置否则不会显示标题
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 设置文字大小
        self.titleLabel.font = [UIFont systemFontOfSize:10];
        // 设置文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        // 设置图片不要拉伸,保持原来的比例
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        // 高亮显示的时候不需要调整图片的颜色
        self.adjustsImageWhenHighlighted = NO;
    }
    
    return self;
}

#pragma mark 设置文字的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    return CGRectMake(0, contentRect.size.height * (kImageRatio + kMarginRatio), contentRect.size.width, contentRect.size.height * kLabelRatio);
}
#pragma mark 设置图片的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return CGRectMake(0, contentRect.size.height * kMarginRatio, contentRect.size.width, contentRect.size.height * kImageRatio);
}
@end

Viewcontroller

#import "MJViewController.h"
#import "MyCell.h"
#import "Shop.h"

@interface MJViewController ()
@property (nonatomic, retain) NSMutableArray *shops;
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.shops = [NSMutableArray array];
    
    for (int i = 1; i<=33; i++) {
        Shop *shop = [[[Shop alloc] init] autorelease];
        shop.name = [NSString stringWithFormat:@"大衣-%i", i];
        
        shop.icon = [NSString stringWithFormat:@"TM.bundle/tmall_icon_cat_outing_%i.png", i%12+1];
        
        [self.shops addObject:shop];
    }
    
    // 不需要分隔线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.shops = nil;
}

- (void)dealloc {
    self.shops = nil;
    [super dealloc];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.shops.count + kColumn - 1)/kColumn;
}

#pragma mark 每当有新的Cell进入视野范围内时,就会调用这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 先根据标识去缓存池查找Cell对象
    static NSString *identifier = @"MyCell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 说明缓存池中没有可循环利用的Cell
    if (cell == nil) {
        // 创建Cell的时候绑定一个标识
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    
    //0  1  2  3
    //4 
    //
    
    // 从哪个位置开始截取
    int location = indexPath.row * kColumn;
    // 截取的长度
    int length = kColumn;
    
    if (location + length >= self.shops.count) {
        length = self.shops.count - location;
    }
    
    
    NSRange range = NSMakeRange(location, length);
    NSArray *rowShops = [self.shops subarrayWithRange:range];
    [cell setRowShops:rowShops];
    
    return cell;
}

#pragma mark 设置Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kCellHeight;
}
@end