UITableView基本使用(二)
来源:互联网 发布:单片机温度采集系统 编辑:程序博客网 时间:2024/05/17 02:42
上篇博客讲解了UITableView的最基本的使用,这篇博客做一些补充。
有时候我们需要实现这样的功能:
1.比如改变系统自带的选中cell的颜色:
可以在cellForRowAtIndexPath代理方法中创建cell时,自定义一个UIView,frame等于cell.frame,然后设置这个自定义View的背景色。代码如下:
//cell选中时任意颜色 cell.selectedBackgroundView=[[UIView alloc]initWithFrame:cell.frame]; cell.selectedBackgroundView.backgroundColor=[UIColor greenColor];
2.那有时候我们只想在按住cell的时候有自定义颜色,松开后又想让颜色消失(即选中cell时的自定义颜色一闪而过)
那就要在didSelectRowAtIndexPath代理方法中取消选中。代码如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"===第%ld部分, 第%ld行",indexPath.section,indexPath.row); //可以让cell选中的颜色一闪而过 [tableView deselectRowAtIndexPath:indexPath animated:NO];}
3.有时候系统默认高度太小,要改变cell的高度
代码如下:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return new_width(80);//适配不同屏幕}
CGFloat new_Width(CGFloat original){// static dispatch_once_t onceToken;// static CGFloat scale = 0;// dispatch_once(&onceToken, ^{// CGFloat w = [UIApplication sharedApplication].keyWindow.bounds.size.width;// scale = w / 320;// });// return floor(scale * original);// CGFloat scale = [UIScreen mainScreen].scale; return original;}这里注意一下:如果cell的高度是不定的,那应该先计算完高度后再在这里返回
4.cell右边显示一个箭头
//右边箭头 //cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
5.像微信,扣扣都有侧滑删除功能,那如何实侧滑删除某个cell呢。
要实现如下的代理方法:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //删除cell if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dataSourceArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }}//默认是delete,要想变成中文得实现这个方法-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删除";}
有兴趣的可以看看下面完整的代码:
#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,strong)UITableView *tableView;@property(nonatomic,strong)NSMutableArray *dataSourceArray;@end@implementation ViewController-(UITableView *)tableView{ if(!_tableView) { _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource=self; _tableView.delegate=self; [self.view addSubview:_tableView]; _tableView.backgroundColor=[UIColor whiteColor]; } return _tableView;}-(NSMutableArray *)dataSourceArray{ if(!_dataSourceArray) { _dataSourceArray=[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]; } return _dataSourceArray;}- (void)viewDidLoad{ [super viewDidLoad]; [self tableView];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataSourceArray.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellId=@"cellId"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId]; if(!cell) { NSLog(@"只会创建屏幕能够显示的cell的个数"); cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; //cell选中时无颜色 //cell.selectionStyle=UITableViewCellSelectionStyleNone; //cell选中时任意颜色 cell.selectedBackgroundView=[[UIView alloc]initWithFrame:cell.frame]; cell.selectedBackgroundView.backgroundColor=[UIColor greenColor]; //右边箭头 //cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; } cell.textLabel.text=[NSString stringWithFormat:@"第%@行",self.dataSourceArray[indexPath.row]]; return cell;}-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //删除cell if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dataSourceArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }}//默认是delete,要想变成中文得实现这个方法-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删除";}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"===第%ld部分, 第%ld行",indexPath.section,indexPath.row); //可以让cell选中的颜色一闪而过 [tableView deselectRowAtIndexPath:indexPath animated:NO];}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80;}@end效果图:
1 0
- UITableView基本使用(二)
- UITableView的基本使用(二)
- UITableView的基本使用二(性能优化)
- UITableView 基本使用
- UITableView基本使用
- UITableView的基本使用
- UITableView 的基本使用
- 5.UITableView基本使用
- iOS -- UITableView基本使用
- UITableView 的基本使用
- iOS -- UITableView基本使用
- ios控件 UITableView 基本使用
- UITableView的基本使用一
- iOS UITableView的基本使用
- 浅谈UITableview 基本使用
- Swift-UITableView的基本使用
- UITableView基本属性二:编辑模式
- iOS:UITableView 使用(一)--基本使用
- 悟
- 23种设计模式(2):简单工厂模式
- RxJava辅助操作符
- Android Camera
- Scala学习第八弹:递归与尾递归
- UITableView基本使用(二)
- Hashmap和LinkedHashmap的对比
- HTML5新增结构标签
- 细说那些Cookie与Session不为人知的故事
- 顺序表应用5:有序顺序表归并
- GreenDao学习备份
- LeetCode 241. Different Ways to Add Parentheses
- HTML标签使用原则
- php中echo 与print 的区别