iPhone CustomCellを作る。
皆さんこんばんわ
お元気ですか。私はC++のMakefileや便利ツールを聞いて色々と死にそうです。
ファイルの分割方法がよくわかりません。
これはいつか実験して学習するとしましょう。
さて、今回はCutomCellです。表の中をLabel2つにしたい!とか画像入れたい!とかそんなニーズにお答えするセルを作るのがこれです。
まぁセルの中にも色々突っ込んで修正したいわけです。自由がいいんです自由が
さぁいくぞ
プロジェクトの生成、Storyboardの編集
プロジェクト名:CustomCellTest
などなど
とりあえず、画面はこんな感じになります。
その上で、ラベルを追加します。
Viewのタグをそれぞれ1,2と設定します。
ソースコード周り
ファイルを生成します。ObjectiveC-classで大丈夫です。
CustomCell を UITableViewCell
TableView を UITableViewController
として生成してください。
PrototypeCellsのClassにCustomCellをC登録する。
CustomCellにLabelなどを定義する。
定義の仕方はCustomCell.hへLabelなどをcontrolで引っ張って繋ぐ。
最後にidentifierをCellとして登録
ソースコード
CustomCell.h
#import <UIKit/UIKit.h> @interface CustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *TitleLabel; @property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel; @end
Labelなどを紐で繋ぐだけ。
CustomCell.m
こちらは何も弄っていません。最初に生成された通りです。
#import "CustomCell.h" @implementation CustomCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
TableView.h
#import <UIKit/UIKit.h> #import "CustomCell.h" @interface TableView : UITableViewController{ NSArray *TableArray; NSArray *DescriptionArray; } @property (strong, nonatomic) IBOutlet UITableView *myTableView;
テーブルに入れる内容をインスタンス変数として定義する。
TableView.m
#import "TableView.h" #import "CustomCell.h" @interface TableView () @end @implementation TableView - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; TableArray = [[NSArray alloc] initWithObjects:@"YAS",@"AS",@"Nis", nil]; DescriptionArray = [[NSArray alloc] initWithObjects:@"ssS",@"AsS",@"Nis", nil]; // 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; } - (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 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return TableArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if(!cell){ cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.TitleLabel.text = [TableArray objectAtIndex:indexPath.row]; cell.DescriptionLabel.text = [DescriptionArray objectAtIndex:indexPath.row]; // Configure the cell... return cell; }
ViewDidLoad
変数の内容を定義するのみ
numberOfSectionsInTableView
1でおkです。
numberOfRowsInSection
表の長さ
cellForRowAtIndexPath
表の列を確保し、cellの中身があれば、再利用する。
そして、NSArrayで定義した内容をLabelに入れる。
ただ、それだけです。