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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 富贵竹的黄叶子怎么办 土培富贵竹发黄怎么办 富贵竹根变黄了怎么办 富贵竹叶子尖发黄怎么办 富贵竹新叶子发黄怎么办 富贵竹杆变黄了怎么办 水培观音竹叶子发黄怎么办 富贵竹叶子根部发黄怎么办 观音竹叶子干了怎么办 富贵竹叶子发干怎么办 观音竹老叶发黄怎么办 发财树缺营养怎么办 富贵竹根部长芽怎么办 门口的竹子发黄怎么办 富贵竹长根了怎么办 水养竹子太长怎么办 海棠根腐烂了怎么办 水竹根腐烂了怎么办 水培富贵竹黑根怎么办 转运竹根部发黑怎么办 富贵竹跟泡烂怎么办 富贵竹根烂了怎么办 富贵竹的须变黑怎么办 泡富贵竹水发臭怎么办 富贵竹水变黑了怎么办 水观音叶子黄了怎么办 富贵竹根部腐烂发臭怎么办 富贵竹根部发粘怎么办 水竹草叶子发黄怎么办 水竹叶子心发黄怎么办 塔竹叶子发黄怎么办 铁树叶有发黄怎么办 铁树夏天叶发黄怎么办 铁树的叶尖发黄怎么办 铁树叶子发黄是怎么办 榕树盆景长蜜怎么办 六月雪叶子枯萎了怎么办 小叶冬青掉叶子怎么办 榕树叶子发黄落叶怎么办 榕树的叶子发黄怎么办 盆景的叶子发黄怎么办