[Objective-C] UITableViewテンプレート

2016年2月18日

Objective-C テクノロジー プログラミング

スマートフォン独特の表示と言えばTableViewの表示が代表的だと思います。 PCのtable表示はマトリクス表示を基本としますが、スマートフォンは、段落のみの縦方向セグメントです。 重要なのは、サムネイルやそこから多階層展開するようなスマホ独特の挙動や情報量です。 「UITableVIew」の基本的な構造を押さえてコピペでも使えるようにテンプレートを置いておきます。

テンプレートコード

viewController.h

@interface viewController : UIViewController< UITableViewDelegate, UITableViewDataSource >

viewController.m

UITableView *tb = [[UITableView alloc] init]; tb.scrollEnabled = YES; tb.delegate = self; tb.dataSource = self; tb.bounces = YES; //marginの左側を無くす対応 tb.separatorInset = UIEdgeInsetsZero; tb.layoutMargins = UIEdgeInsetsZero; [self.view addSubview:tb]; //tableの高さ(pixel) -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50.0; } //tableに含まれるセクションの数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //セクション内のcell数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } //cellの単体処理 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.textLabel.font = [UIFont boldSystemFontOfSize:12.f]; cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10.f]; } //アクセサリー ">" cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //cellで表示するテキスト cell.textLabel.text =@"text"; //サムネイル画像 cell.imageView.image = [UIImage imageNamed:@"hoge.png"]; //iOS7移行の左側のmarginが空いてしまう事象に対応 cell.separatorInset = UIEdgeInsetsZero; cell.layoutMargins = UIEdgeInsetsZero; return cell; } //編集モード時で、Delete、Insertされた時に呼び出される。 -(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{ } //対象ファイルを削除する。 -(void)delTableLists:(NSIndexPath*)indexPath{ } //セルタップ時に呼び出される。[セルタップ時に行いたい処理を実装する] -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ } //アクセサリボタンタップ時に呼び出される。 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ }

利用方法について

webからリストを取得してtableViewで表示するような使い方が想定されます。 リストの扱いは別途処理を行わないといけないのですが、DataBase、Web-Api、各種センサーからの情報など、色々考えられるし、アプリの品質でもあるので、設計をしっかりしましょう。

人気の投稿

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。

ブログ アーカイブ