天天看点

IOS 学习笔记(9)tableView基础

TableView是一个被分成不同部分的滚动视图,每一部分又进一步被分成行,每行是一个UITableViewCell类的实例。可以把图片,文本和其他任何东西嵌入tableView单元格,可以自定义他们的形状,高度,分组或更多。这些分别在UITableViewDataSource和UITableViewDelegate的协议来定义。

#import<UIKit/UIKit.h>

@interface TableViewController:UIViewController

@property(monatomic,strong)UITableView *myTableView;

@end

@implementation TableViewController

@synthesize myTableView;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.myTableView = [ [UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    [self.view addSubview:self.myTableView];

}

这样就创建了一个空白的TableView

UITableViewStylePlain

创建一个没有背景图片的空白 Table View

UITableViewStyleGrouped

创建一个有背景图片和圆角组边框的 Table View,类似于 Settings app。

怎么添加数据呢?

遵循UITableViewDelegate协议:

//设置tableviewCell高度

-(void)viewDidLoad{

    [super viewDidLoad];

    CGRect tableViewFrame = self.view.bounds;

    self.myTableView = [[UITableView alloc]initWithFrame:tableViewFrame style:UITableViewStylePlain];

    self.myTableView.delegate = self;

    [self.view addSubview:self.myTableView];

}

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

    CGFloat result = 20.0f;

    if([tableView isEqual:self.myTableView]){

        result = 40.0f;

    }

    return result;

}

TableView中的单元格的位置由其索引路径展示出来,一个索引路径是section和row索引的组合。section索引是从零开始的。

添加数据:

添加数据还需要遵循UITableViewDataSource协议

添加一下代码

self.myTableView.dataSource = self;

self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

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

    NSInteger result = 0;

    if([tableView isEqual:self.myTableView]){

        result = 3;

    }

    return result;

}

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

    NSInteger result = 0;

    if([tableView isEqual:self.myTableView]){

        switch(section){

            case 0:{

                 result = 3;

                 break;

             }

             case 1:{

                 result = 5;

                 break;

             }

             case 2:{

                 result = 8;

                 break;

             }

        }

   }

   return result;

}

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

    UITableViewCell *result = nil;

    if([tableView isEqual:self.myTableView]){

        static NSString *TableViewCellIdentifier = @"MyCells";

        result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];

        if(result == nil){

            result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];

        }

    result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];

    }

    return result;

}

接收和处理Table view事件

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

    if([tableView isEqual:self.myTableView]){

        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is Selected",(long)indexPath.row,(long)indexPath.section]);

    }

}

在TableView中使用不同种类的附件

result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

创建自定义tableView单元格附件

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

    UITableViewCell *result = nil;

    static NSString *myCellIdentifier = @"SimpleCell";

    result = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier];

    if(result == nil){

        result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier];

    }

    result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];

    UIButton *button = [UIButton buttonWithType:UIButtonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);

    [button setTitle:@"Expand" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];

    result.accessoryView = button;

    return result;

}

-(void)performExpand:(id)paramSender{

    UITableViewCell *ownerCell = (UITableViewCell *)paramSender:superview;

    if(ownerCell != nil){

        NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];

        NSLog(@"Accessory in index path is tapped. indexPath = %@",ownerCellIndexPath);

        if(ownerCellIndexPath.section == 0 && ownerCellIndexPath.row == 1){

        }

    }

}