UITableView汇总

1.声明数据源数组,声明全局的TableView

NSMutableArray *_dataArray;

UITableView *_tableView;

2.实例化_tableView 遵守协议<UITableViewDeleate,UITableViewDataSoure>

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460)];

_tableView.delegate = self;

_tableView.dataSoure = self;

[self.view addSubView:_tableView];

3.TableView的代理函数

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回每行行高
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{//返回每个分区的头标题行高
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{//返回每个分区的脚标题行高
    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//返回分区数
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{//返回每个分区包含多少多少行Cell

return 100;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回cell的样式和展示内容

NSString *identifier = @"ID";//复用标识
   UITableViewCell *cell =[tableView dequdequeueReusableCellWithIdentifier:identifier];

if(!cell){

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"] autorelease];

}

//设置cell的展示内容 系统方法的cell包含textLabel、detailTextLabel

cell.textLabel.text = _dataArray[indexPath.section] ;

cell.detailTextLabel.text = @"";

return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{//自定义头标题的view
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    //或者UIview *v =[headerView autorelease];
    headerView.backgroundColor = [UIColor greenColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//    label.backgroundColor = [UIColor yellowColor];
//    label.text = @"杨大傻";
//    return (UIView *)label;
    return [headerView autorelease];//内存泄露 使用自动release
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//选中表格视图某一行后,触发的方法

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{//cell附属的button 必须有蓝色的按钮出现,才能点击执行这个方法,点出附属按钮
}

- (void)customEditMethod
{//自定义按钮实现编辑方法时,需要设置一个bool型成员变量
    _isEditing = !_isEditing;
    [_tableView setEditing:_isEditing animated:YES];
}

//系统自带的编辑按钮 作用:切换编辑状态和非编辑状态
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//系统自带的编辑按钮时必须按照下面的方式重写编辑函数,固定写法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [myTableView setEditing:editing animated:animated];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//编辑的方法,如果其它的编辑都不设,只设了这一个,那么就会有cell右滑出现删除的按钮,也是比较常用的
    if (editingStyle == UITableViewCellEditingStyleDelete){
        //删除
        [[dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];  //删除数据源记录
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//删除cell
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        //插入
        [[dataArr objectAtIndex:indexPath.section] insertObject:@"a new cell" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{//删除按钮的字
    return @"删";
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回编辑模式,默认是删除
    if (indexPath.section) {
        return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//移动,不实现这个代理方法,就不会出现移动的按钮
    NSString *text=[[[dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] copy];
    [[dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    [[dataArr objectAtIndex:destinationIndexPath.section] insertObject:text atIndex:destinationIndexPath.row];
    [text release];
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//移动事件
    //通过数据源里找到需要移动的数据
    Student *stu = [[[_dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] retain];//retain为插入保留引用计数
    //从原位置删除
    [[_dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    //插入到新位置
    [[_dataArr objectAtIndex:destinationIndexPath.section] insertObject:stu atIndex:destinationIndexPath.row] ;
    [stu release];//retain加的1 给release了
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以编辑的(增,删)
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以移动的
    return YES;
}

4.在创建cell时也可以自定义cell的样式,先创建好cell的模型,在继承自UITableViewCell 的类中,用Xib创建好Cell的界面 将cell所要显示的控件声明成成员变量,便于接受外界传来的数据。引入cell的模型头文件后,在创建Cell的代理函数中,这样创建:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回cell的样式和展示内容

MyTableViewCell *cell = [tableView dequdequdequeueReusableCellWithIdentifier:@"ID"];
        if (!cell) {
            cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"]autorelease];
        }
    //找到对应的数据模型
    BookModel *bm = [_dataArr objectAtIndex:indexPath.row];
    [cell sendDataModel:bm];
    return cell;
    }
5.cell的多选模式 在返回编辑模式的函数中同时返回 插入|删除

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{//进入可多选模式

return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;

}

6.关闭自动适应

self.automaticallyAdjustsScrollViewInsets= YES;

更加常用的是在设置了ScrollView、TableView、CollectionView时,在viewDidLoad中直接加上适配:

if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        //适配  判断self是否响应setAutomaticallyAdjustsScrollViewInsets: 函数 如果响应 关闭自动适应
    }

7.TableView的索引

在实例化TableView的时候就可以设置索引条属性

_myTableView.tableHeaderView.backgroundColor = [UIColor blueColor];
    //索引条背景色
    _myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //索引条文字颜色
    _myTableView.sectionIndexColor = [UIColor greenColor];

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{//系统方法
    //UITableViewIndexSearch 系统内置的字符串,会显示成搜索小图标
    NSMutableArray *arr = [[NSMutableArray arrayWithObject:UITableViewIndexSearch];
    //NSMutableArray (存放索引标题的数组)
    return arr;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{//因为索引的前面加了个搜索小图标,所以需要重写这个方法点击的时候要返回的数要-1
    return index-1;
}

时间: 2024-07-30 02:17:57

UITableView汇总的相关文章

UIScrollView &amp;&amp; UITableView相关属性汇总

    1, UIScrollView: tracking //当 touch 后还没有拖动的时候值是YES,否则NO zoomBouncing //当内容放大到最大或者最小的时候值是 YES,否则 NO zooming //当正在缩放的时候值是 YES,否则 NO decelerating //当滚动后,手指放开但是还在继续滚动中.这个时候是 YES,其它时候是 NO decelerationRate //设置手指放开后的减速率 maximumZoomScale //一个浮点数,表示能放最大的

UITableView的简单总结与回顾

今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会从九个方面对这个控件做一个简单的综述,希望对大家有帮助吧,嘿嘿. 一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示:          其中左边的是Plain风格的,右边的是Grouped风格,这个区别

GitHub上史上最全的Android开源项目分类汇总

今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源项目分类汇总,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫的界面特效设计.个性化控件.工具库.优秀的Android开源项目.开发测试工具.优秀个人和团体等.可以这样说,每一位Andorid开发人员都能从中找到一个或多个适用自己项目的解决方案,消化吸收并加以利用,可以为自己的APP增色不少.文章最后还列出了部分国外著名Android开发者的信息,包括GitHub地址

iOS、mac开源项目及库汇总

UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看“使用方法”. (国人写) XHRefreshControl – XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHou

最全面的iOS和Mac开源项目和第三方库汇总

UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明.具体使用看“使用方法”. (国人写) XHRefreshControl – XHRefreshControl 是一款高扩展性.低耦合度的下拉刷新.上提加载更多的组件.(国人写) CBStoreHou

【Android】开源项目汇总-备用

from://http://www.eoeandroid.com/home.php?mod=space&uid=765778&do=blog&id=47674 Android开源项目第一篇--个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.其他Android开源项目第二篇--工具库篇  包括依赖注入.图片缓存.网络相关.数据库ORM工具

超全!iOS 面试题汇总

超全!iOS 面试题汇总 2015-10-20 CocoaChina 作者:Job_Yang 之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和重排,现在分享给大家.(题目来源于网络,侵删) 1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承;可以实现多个接口,通过实现多个接口可以完成C++的多重继承;Category是类别,一般情况用分类好

【Anroid】Android开源项目分类汇总

Android开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style.其他Android开源项目第二篇——工具库篇  包括依赖注入.图片缓存.网络相关.数据库ORM工具包.Android公共库.高版本向

汇总一下iOS6,iOS7的新特性

汇总一下iOS6,iOS7的新特性时间 2014-02-01 23:07:48  CSDN博客原文  http://blog.csdn.net/ioswyl88219/article/details/18896657主题 iOS开发iOS6新特性 每次ios大版本的更新,都会带来一些新的东西,对开发者来说,有利有弊. 好处是,新增了很多新的属性,控件和api,开发者权限更大了,可以轻松实现更多的功能.弊端在于,可能废除了一些旧的api接口,需要做更多的适配和兼容.通过自己开发过程中的一些经验,查