UITableView小结

试着总结一下UITableView的的知识点看看自己记得多少, 如有错误请指正

使用tableView需要一个数据源和一个处理tableView事件的代理, 数据源需要遵循协议<UITableViewDataSource>, 代理需要遵循协议 <UITableViewDelegate>, 一般情况下tableView的代理为它自身所在的视图控制器

<UITableViewDataSource>有两个必须实现的方法:

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

返回每个section内有多少行:

1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2 {
3     return _tableArray.count;
4 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

当前屏幕范围内每个cell的内容,每显示一个cell, 数据源的代理会调用一次这个方法并从数据源中取出相应的数据给对应的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify"; // 定义一个字符串作为重用池的标示
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];  // 尝试从重用池中取出一个已存在的cell来使用
    if (!cell) { // 如果重用池中没有就创建一个新的
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
    }
    return cell;
}

UITableViewDataSource还有其他一些方法:

//********//

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

返回一个tableView中有多少个section, 即多少个分组

//********//

 

//*******//

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;  

设置section的头部标题

//******//

//*****//

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

设置section的尾部标题

//********//

//********//

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 

如果tableView的样式是分组的话使用这个方法来设置分组的个数

//********//

等等.... 

 

UItableView的编辑模式(添加, 删除, 移动等)

首先需要开启VC的编辑模式

比如:点击按钮进入编辑模式

 1 - (void)editAction:(id)sender
 2 {
 3     if (self.editing == NO) {
 4         self.editing = YES;
 5         [_tableView setEditing:YES animated:YES];
 6     } else {
 7         self.editing = NO;
 8         [_tableView setEditing:NO animated:YES];
 9     }
10 }

再使用方法判断某个cell能否被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

1 #pragma mark - 控制cell能否被编辑
2 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4 //    if (indexPath.row == 0) {
5 //        return YES;
6 //    }
7     return YES; // 直接return YES让所有cell都可编辑
8 }

 

实现下面的方法来设置cell的编辑样式(删除, 添加....)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
1 #pragma mark - 控制cell的编辑样式
2 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     if (indexPath.row == 0) { // 指定位置cell的编辑样式
5         return UITableViewCellEditingStyleInsert;
6     }
7 //    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; 可以连写, 多选
8     return UITableViewCellEditingStyleDelete;
9 }

 

实现下面的方法提交编辑结果

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

 1 #pragma mark - 提交编辑
 2 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4      5     if (editingStyle == UITableViewCellEditingStyleDelete) { // 如果是删除
 6         [_tableArray removeObjectAtIndex:indexPath.row];
 7         [tableView reloadData]; // 修改后重新载入tableView的数据
 8     }
 9     if (editingStyle == UITableViewCellEditingStyleInsert) {
10         Sutdent *stu = [Sutdent studentWithImage:nil title:@"张三" phone:@"11453455" sex:@""];
11         [_tableArray addObject:stu];
12         [tableView reloadData];
13         
14     }
15 }

实现下面的方法实现移动cell:

- (void)tableView:(UItableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

 

 1 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 2 {
 3     NSDictionary *sDic = _tableArr[sourceIndexPath.section];
 4     NSMutableArray *sArr = [sDic[kArray] mutableCopy];
 5     
 6     if (sourceIndexPath.section == destinationIndexPath.section) { // 如果目标位置与原位置处在同一分组
 7         BookDataSource *bookData = sArr[sourceIndexPath.row];
 8         [sArr removeObject:bookData];
 9         [sArr insertObject:bookData atIndex:destinationIndexPath.row];
10         [sDic setValue:sArr forKey:kArray];
11         [_tableView reloadData];
12     } else {
13         
14         NSDictionary *dDic = _tableArr[destinationIndexPath.section];
15         NSMutableArray *dArr = [dDic[kArray] mutableCopy];
16         
17         BookDataSource *sBook = sArr[sourceIndexPath.row];
18         [sArr removeObjectAtIndex:sourceIndexPath.row];
19         [dArr insertObject:sBook atIndex:destinationIndexPath.row];
20         
21         [sDic setValue:sArr forKey:kArray];
22         [dDic setValue:dArr forKey:kArray];
23         
24         [_tableView reloadData];
25     }
26 }

 

tips :

1>自定义cell , 新建类继承与UITableViewCell类 , 在该cell中添加视图 使用 [self.contentView addSubview:view];

其他关于Tableview的东西还没有用到以后再慢慢添加

 

posted @ 2014-06-08 15:17  性感的秋波  阅读(224)  评论(0编辑  收藏  举报