iOS- UITableView

UITableView继承自UIScrollView,所以可以滚动。

UITableView中的每一条数据都显示在UITableViewCell中,以section显示数据,其中每一行称为row,从0开始。

section(区号)和row(行号)组成NSIndexPath

创建UITableView:

<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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值