UILabel和UITableview自定义分割线

1、到了iOS8上,发现uitable是越来越不会用了;不说了,先看一下截屏效果:

设计期望的效果是:

1,自定义一个单元格,背景是黄色的;期望铺满整个表格单元;

2,单元分割线是贯通;


现实与理想的差别如下:

1,黄颜色单元格没能横向铺满整个单元;

2,分割线右侧没有拉到头;

3,我只花了三个单元格;没有充满的空间,OS里也画上了分割线。


这是我的解决方法:

解决问题1的方式是:

设置table的边距:[tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];

这是iOS8上新出的特性,所以这个问题在iOS7上没有的。


解决问题2和3的方式是:

第一步:设置统一设置表格的分割线留空     [tableView setSeparatorInset:UIEdgeInsetsMake(0, 1000, 0, 0)];

这里设置分割线横行留空1000个像素,这个的目的是为了让所有的分割线画到视图之外,这样子可以让iOS自动补上的分割线都不可见了。


第二步:针对每个单元格设置留空:

    [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];

这样可以让我们需要的单元格分割线回到视图中。


  1. #import "ViewController.h" 
  2.  
  3. #import "DemoCell.h" 
  4.  
  5. @interface ViewController () 
  6.     IBOutlet UITableView *tableView; 
  7. @end 
  8.  
  9. @implementation ViewController 
  10.  
  11. - (void)viewDidLoad { 
  12.     [super viewDidLoad]; 
  13.     // Do any additional setup after loading the view, typically from a nib. 
  14.  
  15.     //以下两行为关键代码 
  16. <strong>    [tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)]; 
  17.     [tableView setSeparatorInset:UIEdgeInsetsMake(0, 1000, 0, 0)];</strong
  18.      
  19.     [tableView setSeparatorColor:[UIColor redColor]]; 
  20.  
  21. - (void)didReceiveMemoryWarning { 
  22.     [super didReceiveMemoryWarning]; 
  23.     // Dispose of any resources that can be recreated. 
  24.  
  25. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  26.     return 3
  27.  
  28. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  29.     DemoCell *cell =  (DemoCell*)[[[NSBundle mainBundle] loadNibNamed:@"DemoCell" owner:self options:nil] lastObject]; 
  30.      
  31.     //以下两行为关键代码 
  32. <strong>    [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; 
  33.     [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)]; 
  34. </strong
  35.     return cell; 
  36.  
  37. @end 

 

 

 

2、产品设计今天要求cell的分割线不能是整个屏幕的长度,这时候我们有两种方法可以解决

方法一就是自己写一个label放在cell上

方法二就是自定义一个分割线,方法如下

首先我们要去掉cell默认的分割线,设为none

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

然后实现- (void)drawRect:(CGRect)rect方法

- (void)drawRect:(CGRect)rect{

//首先获得上下文

CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); 

  CGContextFillRect(context, rect);

//定义分割线的颜色

CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);

//定义分割线的坐标

CGContextStrokeRect(context, CGRectMake(125, rect.size.height, rect.size.width - 10, 1));

这里改一下纵坐标(-1)就是上分割线

}


也可以参考这里一些专业说法:http://www.awnlab.com/archives/2463.html

 

#import "ViewController.h"

#import "DemoCell.h"

@interface ViewController ()
{
    IBOutlet UITableView *tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //以下两行为关键代码
<strong>    [tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    [tableView setSeparatorInset:UIEdgeInsetsMake(0, 1000, 0, 0)];</strong>
    
    [tableView setSeparatorColor:[UIColor redColor]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCell *cell =  (DemoCell*)[[[NSBundle mainBundle] loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
    
    //以下两行为关键代码
<strong>    [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
</strong>
    return cell;
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值