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

IOS开发之UITableView的奇技

2016-03-29 11:32 537 查看
作者:Biaoac

age:保密

sex:直男

性格:低调沉稳,乖张内涵


博客背景:之前一直在使用UITableView,但是一直都只是初识,后来在不断的使用中找到了很多之前没有在意的东西,遂整理出来,当然,有很多还是看别人的博客中提到的点,我把他重踩一遍;

1.点击的时候显示选中状态,但状态一直村在,必须在点击下一个的时候取消选中状态

点击cell的时候调用

- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;


//离开点击时调用,
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

一般用法是在didSelectRowAtIndexPath方法中加入
[tableView deselectRowAtIndexPath:indexPath animated:YES];
即点击cell时cell有背景色,如过没有选中另一个,则这个cell背景色一直在,
加入这句话效果是在点击结束后cell背景色消失。

- (void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath2.



2.cell选中时的背景颜色(默认灰色,现在好像只有无色和灰色两种类型了)


@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;

UITableViewCellSelectionStyleNone,

UITableViewCellSelectionStyleBlue,

UITableViewCellSelectionStyleGray,

UITableViewCellSelectionStyleDefault

重要属性:

indexpath.row:行indexpath.section:组

separatorColor:分割线的颜色separatorStyle:分割线样式

用来自定义头部和尾部自定义view只需要设置高度,宽度设置无效

tableHeaderView;

tableFooterView;

table中展示的数据是从模型(或者其他来源-数据源)中取得的,因此要更改表格数据,只需要更改数据源中得数据,并刷新表格即可。(不要直接更改tableviewcell中textLabel中得text数据,这样上下拖动tableview还是会显示原来的数据)

UITableViewCell的常用属性

accessoryType:cell右边的指示器

accessoryView:可自定义右边的view

backgroundView:自定义背景view

backgroundColor//优先级低于backgroundView

selectedBackgroundView

3.显示右侧索引

- (NSArray )tableView{
NSMutableArray *indexs = [[NSMutableArray alloc] init];
for (int i = 0; i < kHeaderTitle.count; i++) {
[indexs addObject:kHeaderTitle[i]];
}
return indexs;
}


4.删除操作



一般这种Cell如果向左滑动右侧就会出现删除按钮直接删除就可以了。其实实现这个功能只要实现代理方法,只要实现了此方法向左滑动就会显示删除按钮。只要点击删除按钮这个方法就会调用。
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)
editingStyle forRowAtIndexPath:(NSIndexPath )indexPath;

- (void)tableView:(UITableView )indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_titleArray removeObject:_titleArray[indexPath.row]];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationBottom];
}
}


5.导航栏图片拉伸放大



01
还是创建控制器,控制器里面创建tableView,初始化其必要的代理方法使能其正常显示.

02
初始化tableView的时候让tableView向下偏移(偏移下来的那段放图片):
_tableView.contentInset = UIEdgeInsetsMake(backGroupHeight - 64, 0, 0, 0);

03
初始化图片,注意图片的frame设置,加载在tableView上
imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0,
-backGroupHeight, kDeviceWidth, backGroupHeight)];
imageBg.image = [UIImage imageNamed:@"bg_header.png"];
[_tableView addSubview:imageBg];

04
根据滑动时的偏移量改变图片的frame,改变navigationBar的透明度
- (void)scrollViewDidScroll:(UIScrollView )scrollView{
CGFloat yOffset = scrollView.contentOffset.y;
CGFloat xOffset = (yOffset + backGroupHeight)/2;
if (yOffset < -backGroupHeight) {
CGRect rect = imageBg.frame;
rect.origin.y = yOffset;
rect.size.height = -yOffset;
rect.origin.x = xOffset;
rect.size.width = kDeviceWidth + fabs(xOffset)2;
imageBg.frame = rect;
}
CGFloat alpha = (yOffset + backGroupHeight)/backGroupHeight;
[self.navigationController.navigationBar setBackgroundImage:
[self imageWithColor:[[UIColor orangeColor] colorWithAlphaComponent:alpha]]
forBarMetrics:UIBarMetricsDefault];
titleLb.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:alpha];
}

05
渲染navigationBar颜色方法
- (UIImage )imageWithColor:(UIColor )color{
//描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//开启位图上下文
UIGraphicsBeginImageContext(rect.size);
//获取位图上下文
CGContextRef content = UIGraphicsGetCurrentContext();
//使用color演示填充上下文
CGContextSetFillColorWithColor(content, [color CGColor]);
//渲染上下文
CGContextFillRect(content, rect);
//从上下文中获取图片
UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();
//结束上下文
UIGraphicsEndImageContext();
return currentImage;
}


6.刷新界面

No。1
.刷新整个TableVIew
[self.tableView reloadData];


注意:此处reloadData是刷新整个UITableView,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloadData方法,虽然用户看不出来,但是有些浪费资源。


NO.2
刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]
withRowAnimation:UITableViewRowAnimationFade];

注意:这是刷新第一个section的第一个cell很方便的一种方法,虽然看上去,代码变多了,但是很节省资源,尽量减少刷新频率,这也是在iOS开发中对UITableView的一种优化。

NO.3
局部刷新Section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
上面这段代码是刷新第0个section。

NO.4
刷新动画
刷新UITableView还有几个动画:
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade, //淡入淡出
UITableViewRowAnimationRight, //从右滑入 // slide in from right (or out to right)
UITableViewRowAnimationLeft, //从左滑入
UITableViewRowAnimationTop, //从上滑入
UITableViewRowAnimationBottom, //从下滑入
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100// available in iOS 5.0. chooses an appropriate animation style for you
};


在此感谢

http://blog.treney.com

的厚赠;

http://blog.devtang.com

解惑之。。。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: