簡(jiǎn)單代碼實(shí)現(xiàn)九宮格

利用 UITableView 實(shí)現(xiàn)九宮格布局。具體特點(diǎn)如下:?

1、通過(guò)KVC的方法方便實(shí)現(xiàn)了九宮格,簡(jiǎn)便了實(shí)現(xiàn)的代碼;?

2、九宮格顯示圖片的代碼,縮小截取固定大小的小圖片節(jié)省內(nèi)存;?

3、充分利用了分類(lèi)來(lái)實(shí)現(xiàn)九宮格;?

4. 每個(gè)格子都支持點(diǎn)擊動(dòng)作。


#import "ViewController.h"

#import "UITableGridViewCell.h"

#import "UIImageButton.h"

#define kImageWidth? 100 //UITableViewCell里面圖片的寬度

#define kImageHeight? 100 //UITableViewCell里面圖片的高度

@interface ViewController ()

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIImage *image;

@end

@implementation ViewController

- (void)viewDidLoad

{

? ? [super viewDidLoad];

? ? self.title=@"九宮格";

? ? self.view.backgroundColor = [UIColor whiteColor];


? ? self.image= [selfcutCenterImage:[UIImageimageNamed:@"macbook_pro.jpg"]? size:CGSizeMake(100,100)];


? ? CGSizemSize = [[UIScreenmainScreen]bounds].size;

? ? CGFloatscreenWidth = mSize.width;

? ? CGFloatscreenHeight = mSize.height;

? ? self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];

? ? [self.view addSubview:_tableView];

? ? self.tableView.dataSource = self;

? ? self.tableView.separatorColor = [UIColor clearColor];

? ? self.tableView.delegate=self;

? ? self.tableView.backgroundColor = [UIColor clearColor];

}

#pragma mark UITable datasource and delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

? ? return 1;

}

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

? ? return 12;

}

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

? ? staticNSString*identifier =@"Cell";

? ? //自定義UITableGridViewCell,里面加了個(gè)NSArray用于放置里面的3個(gè)圖片按鈕

? ? UITableGridViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

? ? if(cell ==nil) {

? ? ? ? cell = [[UITableGridViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

? ? ? ? cell.selectedBackgroundView = [[UIView alloc] init];

? ? ? ? NSMutableArray *array = [NSMutableArray array];

? ? ? ? for(inti=0; i<3; i++) {

? ? ? ? ? ? //自定義繼續(xù)UIButton的UIImageButton 里面只是加了2個(gè)row和column屬性

? ? ? ? ? ? UIImageButton *button = [UIImageButton buttonWithType:UIButtonTypeCustom];

? ? ? ? ? ? button.bounds=CGRectMake(0,0,kImageWidth,kImageHeight);

? ? ? ? ? ? button.center=CGPointMake((1+ i) *5+kImageWidth*(0.5+ i) ,5+kImageHeight*0.5);

? ? ? ? ? ? //button.column = i;

? ? ? ? ? ? [buttonsetValue:[NSNumbernumberWithInt:i]forKey:@"column"];

? ? ? ? ? ? [buttonaddTarget:self action:@selector(imageItemClick:) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? ? ? [buttonsetBackgroundImage:self.image forState:UIControlStateNormal];

? ? ? ? ? ? [celladdSubview:button];

? ? ? ? ? ? [arrayaddObject:button];

? ? ? ? }

? ? ? ? [cellsetValue:arrayforKey:@"buttons"];

? ? }


? ? //獲取到里面的cell里面的3個(gè)圖片按鈕引用

? ? NSArray*imageButtons =cell.buttons;

? ? //設(shè)置UIImageButton里面的row屬性

? ? [imageButtonssetValue:[NSNumbernumberWithInt:indexPath.row]forKey:@"row"];

? ? returncell;

}

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

? ? return kImageHeight + 5;

}

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

? ? //不讓tableviewcell有選中效果

? ? [tableViewdeselectRowAtIndexPath:indexPath animated:YES];

}

-(void)imageItemClick:(UIImageButton*)button{

? ? NSString*msg = [NSStringstringWithFormat:@"第%i行 第%i列",button.row +1, button.column +1];

? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:msg

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cancelButtonTitle:@"好的,知道了"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:nil,nil];

? ? [alertshow];

}

#pragma mark 根據(jù)size截取圖片中間矩形區(qū)域的圖片 這里的size是正方形

-(UIImage*)cutCenterImage:(UIImage*)image size:(CGSize)size{

? ? CGSizeimageSize = image.size;

? ? CGRectrect;

? ? //根據(jù)圖片的大小計(jì)算出圖片中間矩形區(qū)域的位置與大小

? ? if(imageSize.width> imageSize.height) {

? ? ? ? floatleftMargin = (imageSize.width- imageSize.height) *0.5;

? ? ? ? rect =CGRectMake(leftMargin,0, imageSize.height, imageSize.height);

? ? }else{

? ? ? ? floattopMargin = (imageSize.height- imageSize.width) *0.5;

? ? ? ? rect =CGRectMake(0, topMargin, imageSize.width, imageSize.width);

? ? }


? ? CGImageRefimageRef = image.CGImage;

? ? //截取中間區(qū)域矩形圖片

? ? CGImageRefimageRefRect =CGImageCreateWithImageInRect(imageRef, rect);


? ? UIImage*tmp = [[UIImagealloc]initWithCGImage:imageRefRect];

? ? CGImageRelease(imageRefRect);


? ? UIGraphicsBeginImageContext(size);

? ? CGRectrectDraw =CGRectMake(0,0, size.width, size.height);

? ? [tmpdrawInRect:rectDraw];

? ? // 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片

? ? tmp =UIGraphicsGetImageFromCurrentImageContext();


? ? // 使當(dāng)前的context出堆棧

? ? UIGraphicsEndImageContext();


? ? returntmp;

}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容