LoginSignup
3
3

More than 5 years have passed since last update.

Storyboardを使ってViewController+TableView

Last updated at Posted at 2015-07-13

あとで調べたいことがあるのでシンプルに。

Project

  • Xcode 6.4
  • Single View Application

View Controller - View

  • BackgroundをOrange、Alphaを0.8にする。 (Storyboard)
  • Table Viewを乗せる。 (Storyboard)

View Controller - View - Table View

  • 子ViewのBackgroundをGreen、Alphaを0.8にする。 (Storyboard)
  • Separatorの色をBlackにする。 (Storyboard)
  • Constraintは親ViewのCenter合わせで一回り小さく。 (Storyboard)

TableViewの設定

  • Table ViewをView Controllerに繋ぐ。 (Storyboard)
ViewController.h
@property (weak, nonatomic) IBOutlet UITableView *tableView;
  • Table ViewのdataSourceとdelegateをView Controllerに繋ぐ。 (Storyboard)
  • ViewController.hにプロトコルを記述する。 (Code)
ViewController.h
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
  • セル再利用の設定。 (Code)
ViewController.m
#define _CELLID @"cellId"
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:_CELLID];
}
  • UITableViewDataSOurceのrequiredメソッドを追加する。 (Code)
ViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;//適当
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_CELLID forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"Section#%02d Row#%02d", (int)indexPath.section, (int)indexPath.row];
    return cell;
}

ここまで

TestTableView.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3