博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS控件之UITableView使用技巧
阅读量:4046 次
发布时间:2019-05-24

本文共 3315 字,大约阅读时间需要 11 分钟。

在做项目开发时,绝大多数界面都要使用到UITableView,这里介绍一些自己经常用到的一些小技巧.

UITableView *tableView;
UITableViewCell *cell;

1)当 cell 中莫名多了一条灰色线条时,不要慌,只需要一行代码搞定

tableView.separatorStyle = NO; // 去掉分割线

2)当屏幕上cell 无内容时,显示空白

tableView.tableFooterView = [[UIView alloc] init];

3)点击 cell 的时候,取消选中效果

cell.selectionStyle = UITableViewCellSelectionStyleNone;

**另外还有两种选中样式

cell.selectionStyle=UITableViewCellSelectionStyleBlue; //选中时蓝色效果 cell.selectionStyle=UITableViewCellSelectionStyleGray; //选中时灰色效果

4)tableView 添加到控制器 view 上,与 view 背景颜色一样

tableView.backgroundColor = self.view.backgroundColor;
**注意,tableView 分组样式,默认背景颜色带一点浅灰色

另一种方法也可以实现类似效果:

// 非tableView的透明,设置后有Cell的部分不透明,无cell的部分透明(也可设置为类似一个半透明遮罩)
_selectTableV.backgroundView = nil;

_selectTableV.backgroundColor = RGBACOLOR(0, 0, 0, 0);_selectTableV.opaque = NO;

5)tableView 添加到 控制器view 上,有时(tableview的样式为 UITableViewStyleGrouped ),导航栏下面会空出一段距离

self.automaticallyAdjustsScrollViewInsets = NO;
**原因:控制器自动适应滚动视图,将tableView 向下偏移了(20+44)的距离,即 工具栏 + 导航栏 的高度

===========tableView方法巧妙应用=====

tableView滚动到指定位置

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; [tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

**如果默认滚动到某个位置,则不要设置动画效果

=====代理方法的巧妙应用=============

1) tableview的样式为 UITableViewStyleGrouped
当tableView 上方莫名多出来一段空白距离
在代理方法:- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.01;
}

2)禁止某个 cell 的用户交互

- (NSIndexPath )tableView:(UITableView )tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row > 2) {
return indexPath;
}
return nil;
}

3)tableView 分割线左侧总是有一段空白 那么来悄悄实现以下代码即可

/*******左侧缩进为0*********************/

- (void)viewDidLayoutSubviews{    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];    }    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];    }}- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath{    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {        [cell setSeparatorInset:UIEdgeInsetsZero];    }    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {        [cell setLayoutMargins:UIEdgeInsetsZero];    }}

/*******左侧缩进为0*********************/

4)当两个 cell 出现重叠或者部分重叠,在代理方法中实现以下代码

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {    cell.clipsToBounds = YES;}

5)UITableView的Plain风格, 导致SectionTitle, SectionView悬浮在Navigation的下面, 不随着TableView滑动而滑动, 下面代码解决悬浮问题

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {    CGFloat sectionHeaderHeight = 30;    BOOL isUpSectionScrollToNavigation = scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0;    BOOL isDownSectionScrollOutNavigation = scrollView.contentOffset.y >= sectionHeaderHeight;    if (isUpSectionScrollToNavigation) {        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);    }else if (isDownSectionScrollOutNavigation){        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);    }}

转载地址:http://dfwci.baihongyu.com/

你可能感兴趣的文章
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>