TableView 总结

1.TableView图示:

   1style

 

普通: 

分组:

 

 

   2)总体:

 

具体运用:

 

 

   3cell图示:

      自带样式:

       

      

 

 

2.TableViewController

 

     1tableViewController默认的delegatedatasourceself,即所对应类。

 

     2)只有tableViewController能使用static  cells,即直接在storyboard中绘制静态tableview,可连线实现outlet和action

       (设置后不需实现delegate和datasource)

          大部分时候用statictableViewController实现布局和设备匹配十分好用

         

    

 

    且tableViewController中实现的委托会覆盖storyboardstatic设置的内容。

 

      3)去掉多余tableview线

     viewDidLoad里调用:(对应tableview)

     [self setExtraCellLineHidden:self.tableView];

     

  //去掉多余tableview线

  -(void)setExtraCellLineHidden: (UITableView *)tableView

  {

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

      view.backgroundColor = [UIColor clearColor];

      [tableView setTableFooterView:view];

  }

 

 

3.设置TableViewdelegatedatasource

     1)设置delegatedatasource

                storyboard下:

 

 

            代码实现:

     self.rootTableView.delegate = self;

    self.rootTableView.dataSource = self;

    并且加上<UITableViewDelegate,UITableViewDataSource>

 

 2)实现delegatedatasource

 

 

        

        //设置section数目,默认为1

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

    return [self.classDic count];

    

}

 

重要 //设置section对应的row数目(必须实现)

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

    return [self.listItem[section] count];

    

}

 

//设置section对应的title

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

    return self.sectionName[section];

   

}

 

重要 //生成cell(必须实现)

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

   

    static NSString *CellIdentifier= @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

       

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

 

        cell.tag = indexPath.row;

    }

    NSString *name = @"sth";

   

    cell.textLabel.text = name;

    

    return cell;

}

 

//设置row对应的行高(需要变化时才设置)

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    RulesCell *cell = (RulesCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell.frame.size.height;

}

 

     //设置选中行后的响应

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    [self clearCellSeletedStatus];

    RulesCell *cell = (RulesCell *)[tableView cellForRowAtIndexPath:indexPath];

    [cell selectCell];

    self.selectedRulesNumber = indexPath.row + 1;

 

}

    其它代理暂时没用到就不列出。

 

4.设置TableView的自定义cell:(最简单用第一种设计方式)

重要      1)在storyboard中进行:(可设置cell的class进行其他设置)

           

 

重要 拖出cell后自定义,并设置唯一标识identifier,加载cell时用!

(利用autolayout设置好位置关系)

 

注意:cell的高度默认不变,需根据情况自行实现heightForRowAtIndexPath,具体不同参考:http://www.mgenware.com/blog/?p=507

 

补充:

重要 设置cell对应class,映射cell里的subview,调用时就把上面代码的UITableViewCell 改成自己的CustomCell,把CellIdentifier改成自己设置的identifier

 

 

 

(通过[cell viewWithTag:3],利用设置tag也可访问cell中subview)

 

  2)利用xib自定义cell

  1.新建文件:

   

  

 

  2.自定义(不用设置files owner对应的class

     cell对应的class

  

  

 

   3.调用:(import对应cell class

 

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

    

    static NSString *detailIndicated = @"rulesCell";

    RulesCell *cell = [tableView dequeueReusableCellWithIdentifier:detailIndicated];

    

    if (cell == nil) {

        

        //通过xib的名称加载自定义的cell

重要         cell = (RulesCell *)[[[NSBundle mainBundle] loadNibNamed:@"RulesCell" owner:nil options:nil] lastObject];

        cell.tag = indexPath.row;

        

    }

    

    cell.numberLabel.text = [NSString stringWithFormat:@"(%d)",indexPath.row + 1];

    

    [cell setRulesLabelText:self.dataArray[indexPath.row]];

    

    return cell;

}

 

重要 注意:上面加载方法会导致不断重新创建cell,浪费内存,不能达到重用,用以下代码解决:

     static NSString *CustomCellIdentifier = @"RulesCell";

    

    RulesCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];

    

    if (cell == nil) {

        UINib *nib = [UINib nibWithNibName:@"RulesCell" bundle:nil];

        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];

    }

  

补充:

  缓存为空的时候,通过nsBundle,mainBundle获得数组(两种方法)

       //方法1
       NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil];
       cell = objects[0];
      
       //或者:方法2(解决浪费内存问题)
       UINib *nib =  [UINib nibWithNibName:@"tableCell" bundle:nil];//nil则默认为mainBundle
       NSArray *array = [nib instantiateWithOwner:nil options:nil];
       cell = array[0];

 

(另外方法参考:http://blog.csdn.net/yohunl/article/details/19619167)

 

   3)动态cell height:(label自动换行)

         写在cell类中:

//赋值 and 自动换行,计算出cell的高度

-(void)setRulesLabelText:(NSString*)text{

    

    //获得当前cell高度

    CGRect frame = [self frame];

    

    //文本赋值

    self.contentLabel.text = text;

    

    //计算对应文本所需高度

    CGFloat maxWidth = self.contentLabel.frame.size.width - self.contentLabel.font.pointSize;//计算最大宽度(有内边距)

    

    CGSize labelSize = [text sizeWithFont:self.contentLabel.font

                 

                      constrainedToSize:CGSizeMake(maxWidth, 1000) //1000为最高高度

                 

                      lineBreakMode: NSLineBreakByWordWrapping];

    

    

    self.contentLabel.numberOfLines = 0;//表示label可以多行显示

    

    self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;//换行模式,与上面的计算保持一致。(重要)

    

    self.contentLabel.frame = CGRectMake(self.contentLabel.frame.origin.x, self.contentLabel.frame.origin.y, self.contentLabel.frame.size.width, labelSize.height);//保持原来Label的位置和宽度,只是改变高度。

    

    //计算出自适应的高度

    frame.size.height = labelSize.height + self.contentLabel.frame.origin.y*2;

    self.frame = frame;

}

 

最后在tableview中实现代理:

 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    RulesCell *cell = (RulesCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell.frame.size.height;

}

 

 

5.数据来源:

 

     1)通过动态数组NSMutableArray中的数据,来显示数据

 

     2)通过plist文件提供数据,来显示数据,方便分组

             

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *plistPath = [bundle pathForResource:@"content" ofType:@"plist"];

    self.data = [[NSDictionary alloc] initWithContentsOfFile:plistPath];//根据plist文件储存类型变化

 

5.分节表示图添加索引

 

 

 

主要用到的方法:

 

//分节索引设置

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

{

    NSMutableArray *listTitles = [[NSMutableArray alloc]

                                  initWithCapacity:[self.listGroupname count]]; //“A组”改为“A”

    for (NSString *item in self.listGroupname) {

        NSString *title = [item  substringToIndex:1];

        [listTitles  addObject:title];

    }

    return listTitles;

}

 

 

 

 

6.修改单元格

 

1)图示状态:

 

 

2)进入编辑模式:

 

      1.设置editButtonItem

 

        self.navigationItem.rightBarButtonItem = self.editButtonItem;

 

   实现方法:(rightBarButtonItem自动在edit和done状态切换)

       

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {

 

    [super setEditing:editing animated:animated];

    [self.tableView setEditing:editing animated:YES];

    NSLog(@"editing");

}

 

       2.实现左滑删除:

    

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

    return YES;

}

 

 

 

3)实现其他委托:

 

     1.设置编辑类型(deleteinsertnone):

    

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

{

    if (indexPath.row == [self.listTeams count] -1) {

        return UITableViewCellEditingStyleInsert;

        

        

    } else {

        return UITableViewCellEditingStyleDelete;

    }

}

 

  2.实现编辑操作(删除或插入):

 

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

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

 

        [self.listTeams removeObjectAtIndex:indexPath.row];//数据相应处理

       

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//动画效果

        

        

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert) {

         //Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.

        [self.listTeams insertObject:self.txtField.text atIndex:[self.listTeams count]];

        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        

    }

}

 

  3)实现编辑状态下可移动单元格:

 

       1.允许移动:

 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath {

    return YES;

}

 

        2.具体实现移动委托:

   

        - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)

sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    

    [self.listTeams removeObjectAtIndex:sourceIndexPath.row];//先删除

    [self.listTeams insertObject:stringToMove atIndex:destinationIndexPath.row];//后移动

    

}

 

4)效果图:

 

 

 

7.实现下拉刷新

 

  1UITableViewController添加了一个refreshControl属性,这个属性保持了UIRefreshControl的一个对象指针

 

 2)具体代码实例:

 

#import "TableViewController2.h"

 

@interface TableViewController2 ()

 

@property (nonatomic,strong) NSMutableArray* Logs;

 

@end

 

@implementation TableViewController2

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //初始化变量和时间

    self.Logs = [[NSMutableArray alloc] init]; NSDate *date = [[NSDate alloc] init]; [self.Logs addObject:date];

    

    //初始化UIRefreshControl

    UIRefreshControl *rc = [[UIRefreshControl alloc] init];

    rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; [rc addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];

    self.refreshControl = rc;

 

}

 

//刷新请求(UIControlEventValueChanged事件的处理方法)

-(void) refreshTableView

{

    if (self.refreshControl.refreshing) {

        self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加载中..."];

        //添加新的模拟数据

        NSDate *date = [[NSDate alloc] init]; //模拟请求完成之后,回调方法callBackMethod

        [self performSelector:@selector(callBackMethod:) withObject:date afterDelay:3];

    }

}

 

//回调方法

-(void)callBackMethod:(id) obj

{

    [self.refreshControl endRefreshing];//结束刷新

     self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];

    [self.Logs addObject:(NSDate*)obj];

    [self.tableView reloadData];

}

 

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

    return 1;

}

 

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

    return [self.Logs count];

}

 

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

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    

    if (cell == nil) {

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

    }

    

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

    [dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];

    cell.textLabel.text = [dateFormat stringFromDate: [self.Logs objectAtIndex:[indexPath row]]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

 

 

@end

 

3)效果图

 

 

 

posted @ 2015-09-17 10:14  朱#稀饭  阅读(136)  评论(0编辑  收藏  举报