SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
[iOS	
  bestPractice:ViewController
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  with:TVArrayDataSource];
何について発表?
High-qualityViewController
ための一つの方法
iOSのViewControllerが
太っている
イベントの処理
viewのコード
tableviewなどのdelegate
logicのコード
iOSのViewControllerが
太っている
イベントの処理
viewのコード
tableviewなどのdelegate
logicのコード
ファイルの分割
ViewControllerにTableViewのデータソースを
// number of rows
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [itemArray count];
}
// cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"MYCELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
// get item
NSString *item = [itemArray objectAtIndex:indexPath.row];
// config cell
[cell.textLabel setText:item];
return cell;
}
書くことが多い
ViewControllerにTableViewのデータソースを
// number of rows
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [itemArray count];
}
// cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"MYCELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
// get item
NSString *item = [itemArray objectAtIndex:indexPath.row];
// config cell
[cell.textLabel setText:item];
return cell;
}
書くことが多い
データソースを別のクラスに
置きましょう
ViewController 1
TVArrayDataSource
ViewController 2
// number of rows
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [itemArray count];
}
// cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"MYCELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
// get item
id *item = [itemArray objectAtIndex:indexPath.row];
// config cell
..... perform block or selector .....
return cell;
}
// TVArrayDataSourceの初期化
...
// config cell
[cell.textLabel setText:item];
@property (strong, nonatomic) NSArray *itemArray;
// TVArrayDataSourceの初期化
...
// config cell
[cell.imageView setImage:item];
セールの設定の方法
// define block
TVCellConfigureBlock configureCell = ^(UITableViewCell *cell, NSString *name) {
[cell.textLabel setText:name];
};
// create datasource and set configure block
self.dataSource = [[TVArrayDataSource alloc] initWithItems:items
cellIdentifier:@"MYCELL"
cellConfigureBlock:configureCell];
// create datasource and set selector
self.dataSource = [[TVArrayDataSource alloc] initWithItems:items
cellIdentifier:@"MYCELL"
target:self
cellConfigureSel:@selector(configureCell:andItem:)];
- (void)configureCell:(UITableViewCell *)cell andItem:(NSString *)item
{
[cell.textLabel setText:item];
}
block
selector
TVArrayDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (!cell) {
if (self.isCellFromXib) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:self.cellName
owner:self
options:nil];
cell = [nib objectAtIndex:0];
} else {
cell = [[NSClassFromString(self.cellName) alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:self.cellIdentifier];
}
}
// get item
id item = [self.items objectAtIndex:indexPath.row];
// configure cell
if (self.isConfigByBlock) {
self.configureBlock(cell, item);
} else {
objc_msgSend(self.target, self.configureSelector, cell, item);
}
return cell;
}
ソースを見ましょう
https://github.com/nghialv/TVDataSource

Weitere ähnliche Inhalte

Andere mochten auch

Web動作確認方法
Web動作確認方法Web動作確認方法
Web動作確認方法
s2team
 
iOS動作確認方法
iOS動作確認方法iOS動作確認方法
iOS動作確認方法
s2team
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
s2team
 
S2 team
S2 teamS2 team
S2 team
s2team
 
Guide to obtaining bts3900 information(v1.0.2)
Guide to obtaining bts3900 information(v1.0.2)Guide to obtaining bts3900 information(v1.0.2)
Guide to obtaining bts3900 information(v1.0.2)
Oyekunle Abiodun
 

Andere mochten auch (16)

Esimiehemme valmentavat ja innostavat alaisiaan
Esimiehemme valmentavat ja innostavat alaisiaanEsimiehemme valmentavat ja innostavat alaisiaan
Esimiehemme valmentavat ja innostavat alaisiaan
 
Hallituksemme hyväksyy strategian päädraiverit
Hallituksemme hyväksyy strategian päädraiveritHallituksemme hyväksyy strategian päädraiverit
Hallituksemme hyväksyy strategian päädraiverit
 
Kirjoitamme yrityksemme historian vuosittain slideshare
Kirjoitamme yrityksemme historian vuosittain slideshareKirjoitamme yrityksemme historian vuosittain slideshare
Kirjoitamme yrityksemme historian vuosittain slideshare
 
Keskustelemme palkitsemisesta
Keskustelemme palkitsemisestaKeskustelemme palkitsemisesta
Keskustelemme palkitsemisesta
 
Web動作確認方法
Web動作確認方法Web動作確認方法
Web動作確認方法
 
Määritämme hallituksen näköalan
Määritämme hallituksen näköalanMääritämme hallituksen näköalan
Määritämme hallituksen näköalan
 
Esitämme strategian helppotajuisesti
Esitämme strategian helppotajuisestiEsitämme strategian helppotajuisesti
Esitämme strategian helppotajuisesti
 
Tilitoimiston hinnoittelu
Tilitoimiston hinnoitteluTilitoimiston hinnoittelu
Tilitoimiston hinnoittelu
 
iOS動作確認方法
iOS動作確認方法iOS動作確認方法
iOS動作確認方法
 
PLEATS......
 PLEATS...... PLEATS......
PLEATS......
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
S2 team
S2 teamS2 team
S2 team
 
Tunnistamme strategisen elinvoiman vaiheen
Tunnistamme strategisen elinvoiman vaiheenTunnistamme strategisen elinvoiman vaiheen
Tunnistamme strategisen elinvoiman vaiheen
 
Tilitoimiston strategiamalli
Tilitoimiston strategiamalliTilitoimiston strategiamalli
Tilitoimiston strategiamalli
 
Maailman parhaat opit ja työkalut kehittymisen tueksi
Maailman parhaat opit ja työkalut kehittymisen tueksiMaailman parhaat opit ja työkalut kehittymisen tueksi
Maailman parhaat opit ja työkalut kehittymisen tueksi
 
Guide to obtaining bts3900 information(v1.0.2)
Guide to obtaining bts3900 information(v1.0.2)Guide to obtaining bts3900 information(v1.0.2)
Guide to obtaining bts3900 information(v1.0.2)
 

Cocoa benkyoukai

  • 1. [iOS  bestPractice:ViewController                            with:TVArrayDataSource];
  • 6. ViewControllerにTableViewのデータソースを // number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [itemArray count]; } // cell for row - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MYCELL"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // get item NSString *item = [itemArray objectAtIndex:indexPath.row]; // config cell [cell.textLabel setText:item]; return cell; } 書くことが多い
  • 7. ViewControllerにTableViewのデータソースを // number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [itemArray count]; } // cell for row - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MYCELL"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // get item NSString *item = [itemArray objectAtIndex:indexPath.row]; // config cell [cell.textLabel setText:item]; return cell; } 書くことが多い
  • 9. ViewController 1 TVArrayDataSource ViewController 2 // number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [itemArray count]; } // cell for row - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MYCELL"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // get item id *item = [itemArray objectAtIndex:indexPath.row]; // config cell ..... perform block or selector ..... return cell; } // TVArrayDataSourceの初期化 ... // config cell [cell.textLabel setText:item]; @property (strong, nonatomic) NSArray *itemArray; // TVArrayDataSourceの初期化 ... // config cell [cell.imageView setImage:item];
  • 10. セールの設定の方法 // define block TVCellConfigureBlock configureCell = ^(UITableViewCell *cell, NSString *name) { [cell.textLabel setText:name]; }; // create datasource and set configure block self.dataSource = [[TVArrayDataSource alloc] initWithItems:items cellIdentifier:@"MYCELL" cellConfigureBlock:configureCell]; // create datasource and set selector self.dataSource = [[TVArrayDataSource alloc] initWithItems:items cellIdentifier:@"MYCELL" target:self cellConfigureSel:@selector(configureCell:andItem:)]; - (void)configureCell:(UITableViewCell *)cell andItem:(NSString *)item { [cell.textLabel setText:item]; } block selector
  • 11. TVArrayDataSource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // get cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier]; if (!cell) { if (self.isCellFromXib) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:self.cellName owner:self options:nil]; cell = [nib objectAtIndex:0]; } else { cell = [[NSClassFromString(self.cellName) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier]; } } // get item id item = [self.items objectAtIndex:indexPath.row]; // configure cell if (self.isConfigByBlock) { self.configureBlock(cell, item); } else { objc_msgSend(self.target, self.configureSelector, cell, item); } return cell; }