您的位置:首页 > 产品设计 > UI/UE

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;

_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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息