创建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 {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.imageView.image = [UIImage imageNamed:@"Totoro副本"];
cell.textLabel.text = @"龙猫";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
{
return 150;
}
3.运行程序,结果如下:
转载于:https://www.cnblogs.com/ahtchxw/p/5466609.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)