IOS控件之UITableView使用技巧
2016-07-10 10:55
591 查看
在做项目开发时,绝大多数界面都要使用到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;
5)tableView 添加到 控制器view 上,有时(tableview的样式为 UITableViewStyleGrouped ),导航栏下面会空出一段距离
self.automaticallyAdjustsScrollViewInsets = NO;
**原因:控制器自动适应滚动视图,将tableView 向下偏移了(20+44)的距离,即 工具栏 + 导航栏 的高度
===========tableView方法巧妙应用=====
tableView滚动到指定位置
**如果默认滚动到某个位置,则不要设置动画效果
=====代理方法的巧妙应用=============
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 分割线左侧总是有一段空白 那么来悄悄实现以下代码即可
/[b]*******[/b]左侧缩进为0*********************/
/[b]*******[/b]左侧缩进为0*********************/
4)当两个 cell 出现重叠或者部分重叠,在代理方法中实现以下代码
5)UITableView的Plain风格, 导致SectionTitle, SectionView悬浮在Navigation的下面, 不随着TableView滑动而滑动, 下面代码解决悬浮问题
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 分割线左侧总是有一段空白 那么来悄悄实现以下代码即可
/[b]*******[/b]左侧缩进为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]; } }
/[b]*******[/b]左侧缩进为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); } }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flex 控件的重要属性
- 学习Winform文本类控件(Label、Button、TextBox)
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- C#编写ActiveX网页截图控件
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- 一款超酷的Android自定义加载控件
- Android重写View实现全新的控件
- MFC中动态创建控件以及事件响应实现方法