UITableView继承自UIScrollView,所以可以滚动。
UITableView中的每一条数据都显示在UITableViewCell中,以section显示数据,其中每一行称为row,从0开始。
section(区号)和row(行号)组成NSIndexPath
section(区号)和row(行号)组成NSIndexPath
创建UITableView:
为tableView指定数据源:
<span style="font-size:18px;">UITableView *tableView = [[UITableView alloc] initWithFrame:CGRect(0, 0, 320, 480) style:UITableViewStylePlain];// 初始化时指定大小和样式
[tableView setSeparatorStyle:UITaleViewCellSeparatorStyleSingleLine];// 设置分割线样式
[tableView setRowHeight:100];// 设置行高
[self.view addSubview:tableView];
[tableView release];</span>为tableView指定数据源:
1.签订UITableViewDataSource和UITableViewDelegate协议,(数据与视图的分离控制)该协议中有两个必须实现的方法
2.设置代理人
3.实现协议中的方法
/// 指定tableView要显示多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
/// 每行要显示的cell内容 参数1:cell样式 参数2:cell重用标识
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
重用机制
<span style="font-size:18px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 利用tableView内部的重用池(NSSet) 产生cell
// 创建一个重用标识字符串
NSString *str = @"cellReuse";
// 1.从重用池中取一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
// 2.判断取出的cell是否是空(nil)
if (cell == nil) {
// 如果重用池中没有现成的cell, 就自己创建一个cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
NSLog(@"创建cell");
}
// 获取当前的位置(indexPath)对应的数组元素
NSString *value = [self.array objectAtIndex:indexPath.row];
// 3.给cell的table赋值
[cell.textLabel setText:value];
[cell.imageView setImage:[UIImage imageNamed:@"1.jpg"]];
// 给cell设置辅助视图
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
// 4.返回cell
return cell;
}</span>
本文详细介绍了UITableView的基本使用方法,包括创建过程、数据源设置及代理模式的实现方式,并讲解了如何通过重用机制提高效率。
1223

被折叠的 条评论
为什么被折叠?



