您的位置:首页 > 产品设计 > UI/UE

使用storyboard+继承UITableViewController创建TableView项目

2016-02-17 00:00 465 查看
摘要: 如果要学习TableView,请先看这篇!

本文将介绍如何使用storyboard+继承UITableViewController的方式创建一个最简单的TableView项目。

##第一步 创建项目

首先在Xcode中创建一个Empty Project

在项目中创建一个名为main.storyboard的storyboard文件。

对项目进行设置,把Main Interface设置为刚才创建的main.storyboard。

第二步 对AppDelegate进行设置

把AppDelegate.m中的 application:didFinishLaunchingWithOptions:方法修改为:

//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

##第三步 创建TableViewController子类

在项目中创建一个class,名为MyTableViewController, 该类继承了UITableViewController类。

在viewDidload方法中初始化数据源数据。

实现两个必要的代理方法,以实现数据展示。 具体代码如下:

//MyTableViewController.h
#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController

@end

//MyTableViewController.m
#import "MyTableViewController.h"

@interface MyTableViewController ()
@property(nonatomic, strong) NSArray *dataSourceArray;
@end

@implementation MyTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化数据源数组
self.dataSourceArray = @[@"China", @"Unit Kingdom", @"America", @"Spain", @"Scoland", @"Canada", @"Korea", @"Japan"];
}

//每个section有多少个row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}

//设置每个Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyFirstCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.textLabel.text = [self.dataSourceArray objectAtIndex:indexPath.row];

return cell;
}
@end

第四步 绘制UI

在storyboard中创建一个TableViewController。

在storyboard中设置该TableViewController关联的class是MyTableViewController。

在storyboard中设置该Cell的identifier为MyFirstCell。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: