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

来源:互联网 发布:更改手机号码软件 编辑:程序博客网 时间:2024/05/30 02:52

创建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.运行程序,结果如下:
这里写图片描述

0 0
原创粉丝点击