環境
- Xcode 5.0.2
- ARC ON
- iOSシミュレータ 7.0
ストーリーボードにNavigation ControllerとTable View Controllerを追加する。
Table View Cell: 「Style」から"Subtitle"を選択、「Identifier」に"Cell"と入力する。
この紐付けをしておかないと実行時に下記エラーが吐かれる。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Navigation Item: 「Title」に"日本プロ野球チーム"と入力する。
UITableViewControllerを継承したクラス(今回の例ではViewController)を作成する。
tableView: cellForRowAtIndexPath:メソッドとtableView: viewForHeaderInSection:メソッドはテーブルビュー初回描画時と画面スクロールが発生するたびに呼ばれる。
#import <UIKit/UIKit.h> @interface ViewController : UITableViewController @end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
@private
NSArray *_leagueKinds;
NSArray *_leagues;
NSArray *_leaguePrefixes;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
_leagueKinds = @[@"セントラル・リーグ", @"パシフィック・リーグ"];
_leagues = @[
@[@"ジャイアンツ", @"タイガース", @"ドラゴンズ", @"ベイスターズ", @"カープ", @"スワローズ"],
@[@"バッファローズ", @"ホークス", @"ファイターズ", @"マリーンズ", @"ライオンズ", @"ゴールデンイーグルス"]
];
_leaguePrefixes = @[
@[@"読売", @"阪神", @"中日", @"横浜DeNA", @"広島東洋", @"東京ヤクルト"],
@[@"オリックス", @"福岡ソフトバンク", @"北海道日本ハム", @"千葉ロッテ", @"埼玉西武", @"東北楽天"]
];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_leagueKinds count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_leagues[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// if (!cell) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// }
NSArray *teams = _leagues[indexPath.section];
NSArray *teamPrefixes = _leaguePrefixes[indexPath.section];
cell.textLabel.text = teams[indexPath.row];
cell.detailTextLabel.text = teamPrefixes[indexPath.row];
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *HeaderIdentifier = @"Header";
UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if (!view) {
view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
}
view.textLabel.text = _leagueNames[section];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
@end



0 件のコメント:
コメントを投稿