
其次,检查表视图中是否存在闲置的单元格,如果有取出来,没有则重新创建
第一、UITableViewDatasource 相关的方法
添加数据源的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section// 获得section包含的cell个数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView // Default is 1 if not implemented
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrayList count]
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"the section is %d",indexPath.section)
static NSString *idendifier=@"cellIdentify"
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idendifier]
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idendifier]
}
cell.textLabel.text=[self.arrayList objectAtIndex:indexPath.row]
return cell
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"andy"
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"richard"
}
uitableview实现的数据源方法有:表视图继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动。
UITableViewDatasource:实例化表视图时,必须采用该方法来实现数据源的配置
UITableViewDelegate:表视图的委托方法,一般用于处理表视图的基本样式以及捕捉选中单元格选中事件
表视图的结构:
表视图由头部、尾部视图,中间有一连串的单元格视图
表视图的头部由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置
分组表格由一系列的section视图组成,每一个section又包含一个连续的单元格
每个section视图也由头部视图和尾部视图,通过委托方法代理
cell的使用:
首先定义一个标示符
其次,检查表视图中是否存在闲置的单元格,如果有取出来,没有则重新创建
第一、UITableViewDatasource 相关的方法
添加数据源的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section// 获得section包含的cell个数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView // Default is 1 if not implemented
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrayList count]
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"the section is %d",indexPath.section)
static NSString *idendifier=@"cellIdentify"
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:idendifier]
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idendifier]
}
cell.textLabel.text=[self.arrayList objectAtIndex:indexPath.row]
return cell
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"andy"
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"richard"
}
表视图的编辑、移动、删除等:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index// tell table which section corresponds to section title/index (e.g. "B",1))
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
第二、UITableViewDelegate相关的方法
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0)
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
sample:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC=[[[DetailViewController alloc] init] autorelease]
[self.navigationController pushViewController:detailVC animated:YES]
NSLog(@"clicked")
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
sampleCode:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==1)
{
return 100
}else
{
return 50
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
sample code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)] autorelease]
headerView.backgroundColor=[UIColor yellowColor]
UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 30)] autorelease]
label.text=@"header"
[headerView addSubview:label]
return headerView
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)] autorelease]
headerView.backgroundColor=[UIColor redColor]
UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 30)] autorelease]
label.text=@"foot"
[headerView addSubview:label]
return headerView
iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,微信、QQ、新浪微博等软件基本都有UITableView。UITableView有两种style:
UITableViewStylePlain
UITableViewStyleGrouped。
UITableView中数据只有行的概念,并没有列的概念,因为在手机 *** 作系统中显示多列是不利于 *** 作的。UITableView中每行数据都是一个UITableViewCell,在这个控件中为了显示更多的信息,iOS已经在其内部设置好了多个子控件以供开发者使用。如果我们查看UITableViewCell的声明文件可以发现在内部有一个UIView控件(contentView,作为其他元素的父控件)、两个UILable控件(textLabel、detailTextLabel)、一个UIImage控件(imageView),分别用于容器、显示内容、详情和图片。不同的UItableViewCell不同的类型:
UITableView设置完dataSource后需要实现UITableViewDataSource协议,在这个协议中定义了多种数据 *** 作方法,创建一个简单的联系人管理进行演示:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)