UITableView 介绍

在UITableview的简介:

1. 继承与UIScrollView
2. 展示数据和操作由代理的方式回传
3. 自定义cell进行展示

 

UITableview的重用:

1. UITableView通过重用单元格来达到节省内存的目的:通过为每个单元格指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,以及当单元格滚出屏幕时,允许恢复单元格以便重用.对于不同种类的单元格使用不同的ID,对于简单的表格,一个标识符就够了.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   


 2. UITableview是继承于UIScrollView,它的contentSize的宽一般为frame的宽,高为多个cell、header/footer、section相加的高度,当UIScrollView滚动时为把UITableviewCell改变frame(cell的高度在重用是可能会不断改变大小以适应不同的NSIndexPath)和center,从而实现了利用有限的cell个数展示出更多的内容.  
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath   
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section   

 

UITableview编辑状态:

1. 通过直接设置UITableView的editing属性或向其发送setEditing:animated:消息,可将其置于编辑模式,UITableView接收到setEditing:animated:消息时,会发送同样的消息到所有可见的cell,设置其编辑模式.
self.tableview.editing = YES;//方式一
[self.tableview setEditing:YES animated:YES];//方式二


2. 进入编辑模式后,UITableView向其DataSource发送消息询问每个indexPath是否可编辑,在此方法中对不可以编辑的cell返回NO,可以编辑的cell返回YES,若全部可编辑,可不实现,大部分应用不实现此方法。
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 


3. 然后,UITableView 向其delegate发送消息询问EditingStyle,默认为删除

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


4.当点击”Delete”按钮或者”加号”按钮时,UITableView向其DataSource发送消息,根据传递editingStyle来执行实际的删除或插入操作,其流程是先修改tableView的数据模型,向其中删除或插入对应数据项,然后再调整tableView的显示,删除或插入对应的cell。
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

 

UITableview重排序:

1. 若当前tableView允许重排序,则会在每个cell的右侧出现三条灰色横线的控件,拖动此空间可将cell移动到不同的位置。重排序模式和删除/插入是并行的,即可在显示重排序空间的同时显示删除或插入控件。


2.tableView向其DataSource发送消息,询问每一行是否可显示重排序空间,若为NO,则不显示,若为YES则显示。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
   


3.用户可拖动每行右侧的空间来移动该行的位置。


4.用户拖动某行经过目标行上方时,tableView会向delegate发送消息询问是否可移动到此位置(ProposedIndexPath),若不可移动到此位置则返回一个新的目的indexPath,可以的话直接将ProposedIndexPath返回即可。一般情况下不需实现此方法。
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;     

5. tableView向其DataSource发送消息,在此方法中更改tableView的数据模型,移动里面数据项的位置。      

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

 

UITableview 索引:

要实现索引只需要实现两个代理方法即可

1
2
3
4
5
6
7
8
9
// 返回索引列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self dataSourceIndcies];
}
                                                      
// 返回索引位置
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}

修改索引颜色(私有api):

1
2
3
4
5
6
7
8
9
10
11
12
13
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    for(UIView *indexView in [tableView subviews]){
        if([[[indexView class] description] isEqualToString:@"UITableViewIndex"]){
            [indexView setAlpha:1];
            if([indexView respondsToSelector:@selector(setIndexColor:)]) {
                [indexView performSelector:@selector(setIndexColor:) withObject:[UIColor darkGrayColor]];
            }
            if([indexView respondsToSelector:@selector(setIndexBackgroundColor:)]){
                [indexView performSelector:@selector(setIndexBackgroundColor:) withObject:[UIColor clearColor]];
            }
        }
    }
}

posted on   仅此而已_  阅读(389)  评论(0编辑  收藏  举报

导航

< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5
点击右上角即可分享
微信分享提示