UITableView

UITableView的创建

1
2
3
// UITableViewStylePlain 或者 UITableViewStyleGroup 样式,区别如下

UITableViewController *view = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
  • UITableViewStylePlain

  • UITableViewStyleGrouped


UITableViewDataSource协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 告诉tableView一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

/**
* 告诉tableView第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

/**
* 告诉tableView第indexPath行显示怎样的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

/**
* 告诉tableView第section组的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

/**
* 告诉tableView第section组的尾部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

UITableViewCell

  • UITableViewCell内部有个默认的子视图:contentView

    • contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图

    • 辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:

      • UITableViewCellAccessoryDisclosureIndicator

      • UITableViewCellAccessoryDetailDisclosureButton

      • UITableViewCellAccessoryCheckmark

    • 还可以通过cell的accessoryView属性来自定义辅助指示视图(比如往右边放一个开关)

UITableViewCell的contentView

  • contentView下默认有3个子视图

    • 其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)

    • 第3个是UIImageView(通过UITableViewCell的imageView属性访问)

  • UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置

UITableViewCell的结构:

Cell的重用

  • 问题1: 内存

    • iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
  • 解决方案(重用原理) :

    • 当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
  • 问题2: 多样化

    有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell

  • 解决方案:

    UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

自定义Cell

1
2
3
4
5
6
7
8
9
10
11
12
/** 初始化方法 */
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *identifier = @"message";

AMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell != nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([AMMessageCell class]) owner:nil options:nil] lastObject];
}
return cell;
}

外界使用时直接调用 cellWithTableView: 即可

TableView 实现左划删除功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma mark - 删除功能

/** 只要有这个方法就会实现左划删除方法 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

}

/** 实现这个方法可以自定义左划出来的模块的样式 */
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 从数组中删除该联系人
[self.contacts removeObject:self.contacts[indexPath.row]];

// 删除动画 更新tableView
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

// 重新存储联系人文件
// 找到路径
NSString *contanctsDataPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
contanctsDataPath = [contanctsDataPath stringByAppendingPathComponent:contactsFileName];

// 存档
[NSKeyedArchiver archiveRootObject:self.contacts toFile:contanctsDataPath];
}];
action.backgroundColor = [UIColor purpleColor];

return @[action];
}

创建UITableViewRowAction对象方法中的UITableViewRowActionStyle枚举,UITableViewRowActionStyleDefault 就是 UITableViewRowActionStyleDestructive 都是红色的View 而 Normal 样式如下:

UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){}

文章作者: Ammar
文章链接: http://lizhaoloveit.cn/2014/04/09/UITableView/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ammar's Blog
打赏
  • 微信
  • 支付宝

评论