UITableView的使用

6.14.1 UITableViewDataSource协议方法初始化数据

     //设置组与组之间的间距

    self.tableView.sectionHeaderHeight=5;//footer

   

//将类与代理类建立关联,用代码或者利用连线的方式来实现

self.tableView.dataSource = self;

//设置tableView分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//设置tableview每一组Section的行数

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

//设置cell属性和样式

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//添加右侧索引文字

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

return [self.carsGroups valueForKeyPath:@"title"];

}

/tableView进入编辑状态后,每行cell编辑按钮的样式设置

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

{

if (indexPath.row == 0) { // 第一行是插入样式

return UITableViewCellEditingStyleInsert;

}else{

return UITableViewCellEditingStyleDelete;

}

}

//重写该方法,可实现cell滑动删除操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

//先删除数据模型中的数据

[self.contacts removeObjectAtIndex:indexPath.row];

//再局部刷新tableVewl中的cell信息

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

1.1.1 }UITableViewDetegate协议方法监听操作

//tableview选中监听方法实现

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:

(NSIndexPath *)indexPath{}

//tableview逐行设置cell行高监听方法实现

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:

6.14.2 TableView常用属性

//设置滚动区域的偏移

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

//设置行高

self.tableView.rowHeight = 60;

//设置分割线

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

//设置分割线颜色

self.tableView.separatorColor = [UIColor brownColor];

//cell不允许选中

self.tableView.allowsSelection = NO;

//设置分组行高

self.tableView.sectionHeaderHeight = 50;

//进入编辑状态

self.tableView.editing = YES;

/设置头部和尾部的view,同一个view来设置头部和尾部时,默认只能显示头部的

UISwitch *swithBth = [[UISwitch alloc] init];

UISwitch *swithBth1 = [[UISwitch alloc] init];

self.tableView.tableHeaderView = swithBth;

self.tableView.tableFooterView = swithBth1;

TableView方法

1. 刷新

全部或者局部reload,都是触发tableView:cellForRowAtIndexPath:方法,创建或重用cell,局部刷新cell时还可实现动画效果。而cell.textLabel.text是直接赋值,只是更新了页面上的label属性,不触发任何事件。

reload方法执行后会清空缓存池。

2. 滚动到指定位置

方法一:滚动到指定的单元格

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

//方法二:可滚动到tableView底部,scrollToRowAtIndexPath只能滚动到最后一个cell,

??如果tableview包含tableFooterView则需要自己定位

[UIView animateWithDuration:0.5 animations:^{

CGFloat height = self.tableView.contentSize.height - self.tableView.frame.size.height;

[self.tableView setContentOffset:CGPointMake(0, height)];

}];

CeLL重用

Cell常用属性

/*设置backgroundView和selectedBackgroundView的背景颜色时不要直接设置cell.backgroundView.backgroundColor和cell.selectedBackgroundView.backgroundColor

应该创建ImageView后设置其backgroundColor再赋值*/

UIImageView *view = [[UIImageView alloc] init];

view.backgroundColor = [UIColor greenColor];

cell.backgroundView = view;

UIImageView *view1 = [[UIImageView alloc] init];

view1.backgroundColor = [UIColor blueColor];

cell.selectedBackgroundView = view1;

/在设置了cell.backgroundView的背景颜色同时,也设置了cell.backgroundColor

//此时cell.textLabel.text = @"textLabel";

//如果在cell.backgroundColor = [UIColor redColor];后面,

//则label的背景颜色将会与cell.backgroundColor一致,如果在之前,

//则不会影响到label的背景颜色

cell.backgroundColor = [UIColor redColor];

cell.textLabel.text = @"textLabel";

//设置cell右侧按钮样式

cell.accessoryType = UITableViewCellAccessoryDetailButton;

//自定义cell右侧控件,但需要自己添加监听方法

cell.accessoryView = btn;

/设置cell的选中样式

self.selectionStyle = UITableViewCellSelectionStyleNone;

1 自定义Cell与重用

NSString *ID = @"contact_cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) { //用户不会执行下面的语句

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

}

2. tableview中的静态cell被重用时,只与identifier的设置有关,与tableview 的Content无关,与cell的Style属性也无关。

3. 如果需要自定义cell,需要将style设置成Custom后,再自行拖拽控件

4. 自定义cell类时,需要自行创建一个UITableViewCell的子类,并设置cell的Class为这个子类,用这个子类来管理自定义的视图类,视图类通过Class关联了子类后,便可以进行连线管理了。

5 静态Cell

静态的Cell需要设置tableView的Content属性为Static Cells,然后再设置分组数量,再点击每个分组设置row

时间: 2024-08-04 22:44:50

UITableView的使用的相关文章

iOS开发——项目实战总结&UITableView性能优化与卡顿问题

UITableView性能优化与卡顿问题 1.最常用的就是cell的重用, 注册重用标识符 如果不重用cell时,每当一个cell显示到屏幕上时,就会重新创建一个新的cell 如果有很多数据的时候,就会堆积很多cell.如果重用cell,为cell创建一个ID 每当需要显示cell 的时候,都会先去缓冲池中寻找可循环利用的cell,如果没有再重新创建cell 2.避免cell的重新布局 cell的布局填充等操作 比较耗时,一般创建时就布局好 如可以将cell单独放到一个自定义类,初始化时就布局好

iOS开发tips-神奇的UITableView

概述 UITableView是iOS开发中使用频率最高的UI控件,在前面的文章中对于UITableView的具体用法有详细的描述,今天主要看一些UITableView开发中的常见一些坑,这些坑或许不深,但是如果开发中注意不到的话往往比较浪费时间. 神奇的section header 事情的起因是一个网友说要实现一个类似下图界面,但是不管是设置sectionHeaderHeight还是代理方法中实现func tableView(_ tableView: UITableView, heightFor

ios UISearchDisplayController 实现 UITableView 搜索功能

UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都 封装好了,使用起来非常非常的简单.特别是要实现全屏搜索时使用最多. 全屏搜索的意思是如果你用了  NavigationBar 当点击搜索框时 TableView 会自动弹上去

iOS开发——仿Clear纯手势操作的UITableView

前言 在Clear应用中,用户无需任何按钮,纯靠不同的手势就可以完成对ToDoItem的删除.完成.添加.移动.具体来说,功能上有左划删除,右划完成,点击编辑,下拉添加.捏合添加.长按移动.这里将这些功能实现并记录. 左划删除与右划完成 所谓的左右滑动,就是自定义一个cell然后在上面添加滑动手势.在处理方法中计算偏移量,如果滑动距离超过cell宽度一半,就删除它,或者是为文本添加删除线等来完成它:如果没有超过一半,那么就用动画把cell归位. 效果图如下: 关键代码如下: - (void)ha

iOS UITableView表视图(1)

//在.h文件中声明一下 //例如:@property(nonatomic,strong)UITableView *table; //创建一个UITableView self.table = [[UITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)]; //设置行的高度 self.table.rowHeight = 260.0; //设置分割线的颜色 self.table.separatorColor

UITableView

1.如何设置tableview  每行之间的分割线 self.table.separatorStyle=UITableViewCellSeparatorStyleSingleLine; 2.如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置 法一:完全不变色 cell.selectionStyle  =  UITableViewCellSelectionStyleNone: 法二:变下色马上恢复 [tableView deselectRowAtIndexPath:

iOS开发UI篇—UITableview控件基本使用

一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h> 2 3 @interface NJHero : NSObject 4 /** 5 * 头像 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称 10 */ 11 @property (nonatomic, copy) NSString *name; 12 /** 13 * 描述 1

iOS开发UI篇—UITableview控件简单介绍

一.基本介绍 在众多移动应?用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,?且性能极佳 . UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置. 二.数据展示 UITableView需要?一个数据源(dataSource)来显示数据UITableView会向数据源查询一共有多少行数据以及每?行显示什么数据等 没有设置数据源

iOS 中UITableView的深理解

例如下图:首先分析一下需求:1.根据模型的不同状态显示不同高度的cell,和cell的UI界面. 2.点击cell的取消按钮时,对应的cell首先要把取消按钮隐藏掉,然后改变cell的高度. 根据需求先解决第一个需求,需要两步 当模型数据的属性的status [email protected]"2",不显示取消按钮:status = @"1",显示取消按钮. 1.需要注意的是cell的重用在这里面互有一些影响,所以在自定义cell的模型的setter方法中, 在ce

iOS开发UI篇—实现UItableview控件数据刷新

iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运行界面: 点击选中行: 修改数据后自动刷新: 三.代码示例 数据模型部分: YYheros.h文件 // // YYheros.h // 10-英雄展示(数据刷新) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. A