ios软件开发 设置cell之间的间距
2015-09-20 12:38
411 查看
这两天在项目中遇到一个问题,就是需要将cell之间留出一点间距,一般我们在创建出cell都会有分割线加以区分,所以自己想了一下cell之间的间距如何实现。cell之间的间距实现方法我想到的有两种,首先来说第一种:一、利用UITableCell的UILayer里面的borderWidth属性来设置cell之间的间距。代码实现如下
<pre name="code" class="objc">#import "ViewController.h" @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> { NSArray *colorArray; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //设置cell之间的间隔方法 //第一种:利用系统初始化cell的类UITableViewCell 通过设置CGLayer的borderWidth属性来改变cell之间的间隔 //初始化表视图 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; [self colorArr]; } #pragma mark ---隐藏状态栏 - (BOOL)prefersStatusBarHidden{ return YES; } #pragma mark ---颜色数组 - (void)colorArr{ colorArray = @[[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000],[UIColor colorWithRed:0.800 green:0.400 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:1.000 blue:0.800 alpha:1.000],[UIColor colorWithRed:0.800 green:1.000 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000],[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000]]; } #pragma mark ---tableDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //返回12行 return 12; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellID = @"cellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; //cell选中样式为无 cell.selectionStyle = UITableViewCellSelectionStyleNone; //cell背景颜色随机产生 int index = arc4random()%colorArray.count; cell.backgroundColor = colorArray[index]; //设置cell的拐角 cell.layer.cornerRadius = 5; cell.layer.masksToBounds = YES; //设置cell之间的间距 //根据需求可以设置不同的间距 cell.layer.borderWidth = 2; cell.layer.borderColor = [UIColor whiteColor].CGColor; } return cell; }效果:
没有设置cell之间的间距 设置了cell之间的间距
<img src="http://img.blog.csdn.net/20150920124412520?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="400" alt="" /> <img src="http://img.blog.csdn.net/20150920124531763?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="400" alt="" />
第二种实现办法:通过自定义cell实现
代码如下
1、自定义cell实现方法里面
<pre name="code" class="objc">#import "CustoTableViewCell.h"#define S_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)@interface CustoTableViewCell(){NSArray *colorArray;}@end@implementation CustoTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {colorArray = @[[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000],[UIColor colorWithRed:0.800 green:0.400 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:1.000 blue:0.800 alpha:1.000],[UIColor colorWithRed:0.800 green:1.000 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000],[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000]];//背景颜色随机产生int index = arc4random()%colorArray.count;UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, S_WIDTH, 58)];view.backgroundColor = colorArray[index];[self.contentView addSubview:view];}return self;}2、ViewController.m里面
<span style="color:#333333;"></span><pre name="code" class="objc">#import "ViewController.h"#import "CustoTableViewCell.h"@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];//设置cell之间的间隔方法//第一种:利用系统初始化cell的类UITableViewCell 通过设置CGLayer的borderWidth属性来改变cell之间的间隔//初始化表视图UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];tableView.delegate = self;tableView.dataSource = self;[self.view addSubview:tableView];}#pragma mark ---隐藏状态栏- (BOOL)prefersStatusBarHidden{return YES;}#pragma mark ---tableDelegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//返回12行return 12;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 60;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellID = @"cellID";CustoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];if (!cell) {cell = [[CustoTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];//cell选中样式为无cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消分割线tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//设置cell的拐角cell.layer.cornerRadius = 5;cell.layer.masksToBounds = YES;}return cell;}实现效果如图
<span style="color:#333333;"><img src="http://img.blog.csdn.net/20150920130915269?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="400" alt="" /></span>
相关文章推荐
- iOS:转载:IOS谓词--NSPredicate
- 全面了解 iOS 静态库开发
- Bitcode 一种中间代码 Xcode7 && ios9
- iOS开发推送本地通知
- ios自动监测更新
- iOS:文件归档和解归档的详解和使用
- iOS项目开发实战——使用CoreLocation实现定位
- iOS中按钮不响应点击事件
- iOS性能优化大全
- 盘点iOS9最受期待的10大新功能
- IOS通过sskeychain获取唯一标识
- iOS的run loop小结
- iOS 打电话,发短信,发邮件 方法汇总
- iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/
- iOS开发性能优化的25个tips
- iOS runtime 根据类名获取属性的类型和名称
- iOS: 偏好设置的详解和使用
- iOS中的模糊效果
- 苹果开发 笔记(79)NSData
- iOS: 沙盒的详解和目录的获取