Cocoa benkyoukai
- 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の初期化
!
...
// config cell
[cell.textLabel setText:item];
TVArrayDataSource
@property (strong, nonatomic) NSArray *itemArray;
!
// 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];
}
ViewController 2
// TVArrayDataSourceの初期化
!
...
// config cell
[cell.imageView setImage:item];
// get item
id *item = [itemArray objectAtIndex:indexPath.row];
!
}
// config cell
..... perform block or selector .....
return cell;
- 10. セールの設定の方法
block
// 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];
selector
// 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];
}
- 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;
}