UITableView基本使用(二)
2016-05-02 15:06
309 查看
上篇博客讲解了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);//适配不同屏幕
}
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.比如改变系统自带的选中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
效果图:
相关文章推荐
- MataLab-GUI编程初探
- UIKit Dynamics动力系统入门
- codeforces 667C C. Reberland Linguistics(dp)
- UVa 1594 Ducci Sequence
- leetcode-62. Unique Paths
- POJ 1141 Brackets Sequence 括号匹配 区间DP
- java concurrent 之 SynchronousQueue
- [费用流 线段树] BZOJ 3267 KC采花 && 3272 Zgg吃东西 && 3638 Cf172 k-Maximum Subsequence Sum
- POJ3080 Blue Jeans
- Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验
- C# Queue源码剖析
- Leetcode #347. Top K Frequent Elements 前K高频数 解题报告
- 使用Retrofit时出现 java.lang.IllegalArgumentException: URL query string "t={type}&p={page}&size={count}" must not have replace block. For dynamic query parameters use @Query.异常原因
- CodeForces 666A Reberland Linguistics(DP)
- CodeForces 666A Reberland Linguistics(DP)
- HNOI2016 序列(sequence)<莫队>
- 1382 - The Queue
- NSNotification在UITextField的应用
- UItableview 添加 uisearchController
- [leetcode] 347. Top K Frequent Elements 解题报告