<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

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

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

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));
}



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]];
    }
}



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

iOS7.0 以后的 UILabel 会自动将 Text 行尾的空白字符全部去除,除了常见的半角空格( \0×20 )和制表符 (\t) 之外,全角空格 (\u3000)也被计算在内,甚至连多余的换行符(\r\n)也被自动去除了。
这一点虽然方便直接将控件赋值和无需取值后再 trim ,但是太过智能化 了之后,往往不能满足一些本可以简单实现的需求。

需求1.使用添加\n方式将上下文本连续空两行,即实现文本的2倍行距。
iOS7.0
之前解决办法:在每个换行符后面添加一个空格
即如果要显示为:
aaaaaaa
空行
空行
bbbbbb
使用以下格式进行文本赋值
lbl.text = @"aaaaaaa\n\u0020\n\u0020bbbbbb";
iOS7.0
之后需要增加,不增加则无效
lbl.numberOfLines = 0; // 0
表示行数不固定
lbl.lineBreakMode=UILineBreakModeWordWrap;// 允许换行(可选)

需求2.在所有的UILabeltext后增加一个空格,并使text右对齐。
iOS7.0
之前解决办法:直接在text后增加空格即可,即text在赋值前增加空格。
lbl.text = [NSString stringWithFormat:@"%@%@","aaaaa","\u0020"];
iOS7.0之后需要重写UILabeldrawTextInRect方法,通过缩短默认文本绘制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

附录:
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 的数组

}


iOS开发者交流群:446310206


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




目录
相关文章
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
系统的升级涉及各个架构组件,细节很多。常年累月的修修补补使老系统积累了很多问题。 系统升级则意味着需要repair之前埋下的雷,那为何还要升级,可以考虑以下几个方面 成熟老系统常见问题: 1. 缺乏文档(这应该是大小公司都存在的问题。
591 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在线上往hbase导数据,因为hbase写入能力比较强,没有太在意写的问题。让业务方进行历史数据的导入操作,中间发现一个问题,写入速度太快,并且业务数据集中到其中一个region,这个region无法split掉,处于不可用状态。
1305 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Hbase依赖的datanode日志中如果出现如下报错信息:DataXceiverjava.io.EOFException: INFO org.apache.hadoop.hdfs.server.datanode.DataNode: Exception in receiveBlock for block  解决办法:Hbase侧配置的dfs.socket.timeout值过小,与DataNode侧配置的 dfs.socket.timeout的配置不一致,将hbase和datanode的该配置调成大并一致。
767 0
|
Web App开发 Linux
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能。 free命令:监控Linux内存使用状况。
1122 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operation not perm...
916 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
hadoop服务器更换硬盘操作步骤(datanode hadoop目录${HADOOP_HOME}/bin    日志位置:/var/log/hadoop)1.登陆服务器,切换到mapred用户,执行jps命令,查看是否有TaskTracker进程。
977 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/process umo...
741 0
|
Web App开发 前端开发 数据库
|
Web App开发 前端开发 大数据
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一、概述   多维数据模型是最流行的数据仓库的数据模型,多维数据模型最典型的数据模式包括星型模式、雪花模式和事实星座模式,本文以实例方式展示三者的模式和区别。
735 0
|
Web App开发 前端开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
数据仓库建模:定义事实表的粒度Posted on 2015-08-25 09:03 xuzhengzhu 阅读(28) 评论(0) 编辑 收藏 维度建模中一个非常重要的步骤是定义事实表的粒度。
674 0