iOS UITableViewController基本使用简述

     前阵子在学习iOS开发时使用了UIKit中各种控制器。下面根据一个小Demo来对UITableViewController基本使用进行简介以及使用时的注意点进行一个总结。 

    使用UITableViewController一般是应用界面只需要显示一个表格的情况下比较普遍,而且控制器也已经实现了UITableViewDataSource以及UITableViewDelegate协议,故使用方便。

//
//  WeightViewController.m
//  HomePage
//
//  Created by Caesar on 14-11-14.
//  Copyright (c) 2014年 Caesar. All rights reserved.
//

#import "WeightViewController.h"

@interface WeightViewController ()

@end

@implementation WeightViewController
NSArray* item;
- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    item = [NSArray arrayWithObjects:
                    @"                  男生体重指数",
                    @"等级        单项得分(单位:kg/m2)",
                    @"正常        100           17.9~23.9",
                    @"低重          80          <=17.8",
                    @"超重          80          24.0~27.9",
                    @"肥胖          60          >=28.0",
                    @"                  女生体重指数",
                    @"等级        单项得分(单位:kg/m2)",
                    @"正常        100           17.2~23.9",
                    @"低重          80          <=17.1",
                    @"超重          80          24.0~27.9",
                    @"肥胖          60          >=28.0",
                    nil];
    
    self.navigationItem.title = @"体重指数(BMI)15%权重";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

//返回列表中总共该会有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    return 1;
}
//该方法返回值决定表格中指定分区应该包含多少个元素
//这里我只使用了一个分区,如果是需要多个分区的情况可以分别根据section来指定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    return item.count;
}

//最重要的部分,该方法的返回值决定各个表格行的控件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //必备。为表格行分配一个静态字符串作为标识符,使用重用机制。如果是自定义表格且表格高度动态变化的话最好取消重用机制。
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    
    // 自定义表格部分。如果只使用默认形式则只需要如下指定UITableViewCell格式,默认表格行的三个属性为textLabel、detailTextLabel、image
    // 分别对应UITableViewCell显示的标题、纤细的内容以及左边图标,如果使用自定义表格则指定相应的类进行初始化。
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        
    }	
    //根据表格行好indexPath.row对表格行内容进行设定
    NSUInteger rowNum = indexPath.row;
    cell.textLabel.text = [item objectAtIndex:rowNum];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

// 该返回值决定指定分区的页眉
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger *)section
{
    NSString *title = @"页眉" ;
    return title;
}
// 该返回值决定指定分区的页脚
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger *)section
{
    NSString *footer = @"页脚" ;
    return footer;
}
@end

注意点:
    1、在使用多控制器记得指定表格控制器的风格Plain OR Grouped。
       例如WeightViewController* weight = [[WeightViewController alloc]initWithStyle:UITableViewStyleGrouped];
    2、自定义表格可通过界面设计文件根据动态单元格原型进行定制也可以通过继承UITableViewCell的方式代码定制表格行。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值