在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考:

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博!

1.cell 分割线不全:

解决方案1:

补全分割线

-(void)viewDidLayoutSubviews {
    if ([_listTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_listTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_listTableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [_listTableView setLayoutMargins:UIEdgeInsetsZero];
    }

}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

解决方案2:

UITableViewCell绘制分割线

//第一步:
//UITableView去掉自带系统的分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

//第二步:
//在自定义的UITableViewCell里重写drawRect:方法
#pragma mark - 绘制Cell分割线
- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);

    //上分割线,
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);
    CGContextStrokeRect(context, CGRectMake(0, 0, rect.size.width, 1));

    //下分割线
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博!

2.如何解决iOS headerview与tableview之间距离控制?

view 作为 tableView 的 tableHeaderView,单纯的改变 view 的 frame 是无济于事的,tableView  不会大度到时刻适应它的高度(以后 Apple 会不会改变就不知道了),
所以,如何告诉tableView 它的 tableHeaderView 已经改变了?很简单,就一句话(关键最后一句):
[webView sizeToFit];
CGRect newFrame = headerView.frame;
newFrame.size.height = newFrame.size.height + webView.frame.size.height;
headerView.frame = newFrame;
[self.tableView setTableHeaderView:headerView];
 //这样以后,效果就出来了。不过这种过度显得有些生硬,能不能加一点点动画,让它变得顺眼一些呢?试试下面的代码:
[self.tableView beginUpdates];
[self.tableView setTableHeaderView:headerView];
[self.tableView endUpdates];

3.iOS 9.0之后如何解决点击cell的背景颜色呢?

//改变cell的选中颜色:
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = COLOR_BACKGROUNDVIEW;

4.如何解决cell默认选中行,开发中地区二级经常用到!

// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
// 实现了选中第一行的方法
[self tableView:_mainIndustryTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
//例如:
// 默认下选中状态
- (void)customAtIndex:(UITableView *)tableView
{
    // 默认选中第一行
    [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    if ([tableView isEqual:_mainIndustryTableView]) {
       [self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    }
}

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博!

5.如何解决UILabel 标签后补空格失效 或间隔打空格只显示一个空格?

iOS7.0以后的UILabel会自动将Text行尾的空白字符全部去除,除了常见的半角空格(\0×20)和制表符(\t)之外,全角空格

(\u3000)也被计算在内,甚至连多余的换行符(\r,\n)也被自动去除了。

这一点虽然方便直接将控件赋值和无需取值后再trim,但是太过智能化

了之后,往往不能满足一些本可以简单实现的需求。

倍行距。


iOS7.0之前解决办法:在每个换行符后面添加一个空格


即如果要显示为:


aaaaaaa

空行

空行

bbbbbb

使用以下格式进行文本赋值

lbl.text = @"aaaaaaa\n\u0020\n\u0020bbbbbb";

iOS7.0之后需要增加,不增加则无效

lbl.numberOfLines = 0; // 0表示行数不固定
lbl.lineBreakMode=UILineBreakModeWordWrap;
// 允许换行(可选)

需求2.在所有的UILabel的text后增加一个空格,并使text右对齐。


iOS7.0之前解决办法:直接在text后增加空格即可,即text在赋值前增加空格。


lbl.text = [NSString stringWithFormat:@"%@%@","aaaaa","\u0020"];
iOS7.0之后需要重写UILabel的drawTextInRect方法,通过缩短默认文本绘制Rect的宽度半个字体宽度来实现。(当然也可以在底部铺一个view调整,暨简单又高效)
具体实现代码如下:

文件名:MyLabel.h

#import <UIKit/UIKit.h>

 

@interface MyLabel : UILabel

@end

文件名:MyLabel.m

#import "MyLabel.h"

 

@implementation MyLabel

-(id) initWithFrame:(CGRect)frame { 

  self = [super initWithFrame:frame];

    if(self){

     return self;

    }   

}

 

-(void) drawTextInRect:(CGRect)rect {

  //从将文本的绘制Rect宽度缩短半个字体宽度

  //self.font.pointSize / 2

  return [super drawTextInRect:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width - self.font.pointSize / 2, rect.size.height)];

}
@end

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博!

附录:


UILabel会自动清除的空白字符(UNICODE)


\u0009 CHARACTER TABULATION

\u000A LINE FEED

\u000D CARRIAGE RETURN

\u0020 SPACE

\u0085 NEXT LINE

\u00A0 NBSP

\u1680 OGHAM SPACE MARK

\u180E MONGOLIAN VOWEL SEPARATOR

\u2000 EN QUAD

\u200A HAIR SPACE

\u200B ZERO WIDTH SPACE

\u2028 LINE SEPARATOR

\u2029 PARAGRAPH SEPARATOR

\u202F NARROW NO-BREAK SPACE

\u205F MEDIUM MATHEMATICAL SPACE
\u3000 IDEOGRAPHIC SPACE

6.自定义滑动删除背景及字体颜色如图:

//1.自定义滑动删除背景及字体颜色

//2. 然后实现另一个代理方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
editingStyle = UITableViewCellEditingStyleDelete;//此处的EditingStyle可等于任意 UITableViewCellEditingStyle,该行代码只在iOS8.0以前版本有作用,也可以不实现。
}

//3. 再实现
-(NSArray )tableView:(UITableView )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@”删除” handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
NSLog(@”点击删除”);
}];//此处是iOS8.0以后苹果最新推出的api,UITableViewRowAction,Style是划出的标签颜色等状态的定义,这里也可自行定义

UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

}];
editRowAction.backgroundColor = [UIColor colorWithRed:0 green:124/255.0 blue:223/255.0 alpha:1];//可以定义RowAction的颜色
return @[deleteRoWAction, editRowAction];//最后返回这俩个RowAction 的数组

}

如有问题可关注微博咨询博主,提出更好的建议!

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博!

iOS中 自定义cell分割线/分割线偏移 韩俊强的博客的更多相关文章

  1. iOS中 UITableViewCell cell划线那些事 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang 在开发中经常遇到cell分割线显示不全或者想自定义线的宽高等; 最近总结了一下,希望帮到大家: 1.不想划线怎么办? Table ...

  2. iOS中 Realm的学习与使用 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...

  3. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

  4. iOS 南京互联网大会分享及个人见解 韩俊强的博客

    首先分两大块: 1.如何打造高效/稳定的App (重点): 2.软件自动化测试: 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 每日更新关注:http://we ...

  5. iOS中 扫描二维码/生成二维码详解 韩俊强的博客

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  6. iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客

    近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  7. iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客

    简介: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 / ...

  8. iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...

  9. iOS中 MediaPlayer framework实现视频播放 韩俊强的博客

    iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewControlle ...

随机推荐

  1. [USACO08JAN]跑步Running

    题目描述 The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ...

  2. Linux下date使用

    [root@host1 ~]# date #显示时间 2017年 06月 01日 星期四 17:02:59 CST 以指定格式显示时间: [root@host1 ~]# date +%Y%m%d 20 ...

  3. 谷歌发布 TensorFlow Lite [官方网站,文档]

    机器学习社区:http://tensorflow123.com/ 简介 TensorFlow Lite TensorFlow Lite 是 TensorFlow 针对移动和嵌入式设备的轻量级解决方案. ...

  4. Linux下修改主机IP地址、DNS、主机名的三种方法

    使用root用户登录进入linux,打开进去终端 在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 (最后的eth0是网卡名,我的是Auto_et ...

  5. Image中的alt

    如果图片不存在,默认会显示一个缺失图片,这是不友好的 所以可以加上alt属性. 当图片存在的时候,alt是不会显示的 当图片不存在的时候,alt就会出现 <img src="http: ...

  6. mysql获取某个表的所有字段名

    http://www.netingcn.com/mysql-column-name.html mysql安装成功后可以看到已经存在mysql.information_schema和test这个几个数据 ...

  7. centos 7 破解密码

    CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例. 1.重启开机按esc   2.按e     ...

  8. Node.js 字符串解码器

    稳定性: 3 - 稳定 通过 require('string_decoder') ,可以使用这个模块.字符串解码器(StringDecoder)将缓存(buffer)解码为字符串.这是 buffer. ...

  9. JavaScript switch 语句

    switch 语句用于基于不同的条件来执行不同的动作. JavaScript switch 语句 请使用 switch 语句来选择要执行的多个代码块之一.你可以在JavaScript编程实战中了解怎么 ...

  10. Nginx之(四)工作原理

    众所周知,nginx性能高,而nginx的高性能与其架构是分不开的 4.1 进程模型 Nginx在启动后,会有一个master进程和多个worker进程.master进程主要用来管理worker进程, ...