iOS开发——纯代码界面(UITableViewController)

创建UITableViewController(表视图控制器)

创建一个类TableViewController继承UITableViewController
1、AppDelegate.m中代码如下(记得导入TableViewController不然报错)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    TableViewController *view = [[TableViewController alloc] init];
    self.window.rootViewController = view;
    [self.window makeKeyAndVisible];
    return YES;
}

2.TableViewController.m中,已下有三个方法必须实现。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
(2)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
(3)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

TableViewController.m代码如下:

//用来指定表视图的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    //分区设置为1
    return 1;
}

//用来指定特定分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //设置为20行
    return 20;
}

//配置特定行中的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {

        //单元格样式设置为UITableViewCellStyleDefault
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    //设置单元格中的imageView
    cell.imageView.image = [UIImage imageNamed:@"Totoro副本"];

    //设置单元格中的textLable
    cell.textLabel.text = @"龙猫";

    return cell;
}

//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
{
    //这里设置成150
    return 150;
}

3.运行程序,结果如下:
这里写图片描述

 

posted @ 2016-04-18 17:47  小苇  阅读(238)  评论(0编辑  收藏  举报