在项目开发中我们会常常遇到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. 【Codeforces AIM Tech Round 4 (Div. 2) C】

    ·将排序限制于子序列中,又可以说明什么呢? C. Sorting by Subsequences ·英文题,述大意:       输入一个长度为n的无重复元素的序列{a1,a2……an}(1<= ...

  2. c++ primer第15章这几个例子中的构造函数形式不太理解

    //向基类构造函数传递实参p491 class Bulk_item : public Item_base{ public: Bulk_item(,double disc_rate = 0.0): It ...

  3. Spring中整合Cage,实现验证码功能

    1.pom.xml中添加Cage依赖. <dependency> <groupId>com.github.cage</groupId> <artifactId ...

  4. Linux必备操作vim

    vim被称作为编辑器之神,那么在我们操作linux系统时,进行编辑操作有没有感觉心有余而力不足?今天我讲自己总结的一些vim的操作命令和大家进行一下分享,有不足之处还请指出. vim的三种模式大家还记 ...

  5. 获取X天后的日期

    import java.util.Calendar; import java.util.Date; public class main { public static void main(String ...

  6. json转化为对象数组

    1.ascx传值给aspx aspx页面 <%@ Page Title="" Language="C#" MasterPageFile="~/_ ...

  7. JSTL标签四种判断语句的用法

    一.条件运算符 ${user.gender==1?'男':'女'} 二.if() <c:if test="${2>1}">code..</c:if> ...

  8. 指尖大冒险H5小游戏

    前些天看了一篇很赞的文章,又因为想学习phaser,所以有了这个案例,在线预览可以点下方链接. 本案例中,核心原理是按文章中所提到的内容制作,整体遵循"大道至简"的原则开发,其实是 ...

  9. JavaScript判断不同平台

    function getPlatformType() { let UA = navigator.userAgent; if(/MicroMessenger/i.test(UA)){ return 'w ...

  10. vue-router实现登录和跳转到指定页面,vue-router 传参

    定义路由的时候可以配置 meta 字段: const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, childr ...