TableView的用法

TableView是继承ScrollView,在ScrollView还未出现之前,所有的滑动视图都是用TableView来定制的,TableView是必须要实现它自身的两个代理方法的,下面是TableView的一些基本属性和方法的应用


要注意的是(对于所有的ScrollView及其子类来说):

    第一次添加到控制器视图上的子视图如果是ScrollView的对象(包含ScrollView子类的对象),则视图的内容偏移量会根据是否有导航栏和标签栏进行扩充


 

   表视图创建时的基本样式:

    UITableViewStyleGrouped:分组样式

    UITableViewStylePlain 平铺样式(默认)


    设置tableView的内容偏移量

    tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

    tableView.backgroundColor = [UIColor redColor];

    

    设置数据源

    tableView.dataSource = self;

    

    [self.view addSubview:tableView];

    

    

    处理数据

    

    获取文件路径--方法一

   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];

    

    

    拿到bundle的源路径---方法二

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

    //文件路径

    NSString *filePath = [resourcePath stringByAppendingPathComponent:@"font.plist"];

    

    根据一个文件路径返回一个数组

    self.data = [NSArray arrayWithContentsOfFile:filePath];

    

    

    

}


必须实现的两个代理方法


1-----返回多少行---每一组的行数

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

{

    先拿到小数组

    NSArray *sectionData = self.data[section];

    返回小数组的个数

    return sectionData.count;

}


2----------返回的每一个单元格

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

{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];


    取到小数组

    NSArray *sectionArray = self.data[indexPath.section];

    拿到小数组中对应的元素

    NSString *fontName = [sectionArray objectAtIndex:indexPath.row];

    cell.textLabel.text = fontName;

    cell.textLabel.font = [UIFont fontWithName:fontName size:20];

    return cell;

}


可选的代理方法

1-------返回每一组的头视图标题

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

{

    return [NSString stringWithFormat:@"%ldHeader", section];

}


2---------返回每一组的尾视图标题

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

{

    return [NSString stringWithFormat:@"%ldFooter", section];

}