您的位置:首页 > 产品设计 > UI/UE

UITableView基本用法

2012-07-03 17:37 381 查看
表用于显示数据列表,数据列表中的每一项都由行表示。IOS没有行的限制,行数仅受可用才存储空间的限制,IOS的表只有一列。表视图是显示表数据的试图对象,是UITableView类的一个实例,表中的每个可建行都有UITableViewCell类实现。即一个UITableView实例由若干UITableViewCell组成。表视图不负责存储表中的所有数据,只存储足够绘制当前可见行的数据。每次只加载一屏幕的数据。表视图从遵循UITableViewDelegate协议的对象中获取配置数据,从遵循UITableViewDataSource协议的对象中获得行数据。(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回 tableview 有多少个section
//返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return array.Count;  //默认值为1
}

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回对应的section有多少个元素,也就是多少行。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;  //默认也是1 ,分区中的行的个数
}

(3)-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。其中有一个主标题cell.textLabel 还有一个副标题cell.detailTextLabel,  还有一个 image在最叫cell.imageView.  还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。

//设置每行调用的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";//自定义的标识符

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.imageView.image=image;//未选cell时的图片
cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
cell.text=@”测试文本”;
return cell;
}

表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。




(4)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section返回指定的section的 header的title,如果这个sectionheader  有返回view,那么title就不起作用了。 [code]- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == tableView_)
{
if (section == 0)
{
return @"Girls";
}
else
{
return @"Boys";
}
}
}
(5)设置让UITableView行缩进
// 把每一行的缩进级别设置为其行号,第一行为1,第二行为2
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}

(6)设置cell每行间隔的高度
[/code]
- (CGFloat)tableView:(UITableView *)tableView eightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}

(7)设置选中Cell的响应事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}

该方法是选中之后执行。另一个方法willSelectRowAtIndexPath是在一行选择前调用,通常用来阻止某行的能否被选中

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row == 0)
return nil;//设置第一个可见行不能被选中
return indexPath;
}





图片说明:此图片是从其他网站上戒掉的部分图。和上述代码运行效果不大相同。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息