まず、UITableViewのデータソースとなる保持されたプロパティを作成します。
- (NSInteger)tableViewNumberOfRowsInSection:(UITableView *)tableView {
return dataSource.count; // create cells by count of array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath {
static NSString *reuseIdentifier = @"fruit";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; // add autorelease if not using ARC
//add your UIButton here, give it tag
UIButton *button = ...;//alloc the button and give it proper frame and add it as cell's subview
[cell.contentView addSubview:button];
button.tag = 5;
}
//extract the button and give it title
UIButton *button = (UIButton *)[cell viewWithTag:5];
button.title = [dataSource objectAtIndex:indexPath.row];
return cell;
}
:.hファイル
@property (strong, nonatomic) NSArray *dataSource; //use retain instead of strong if not using ARC
次の二つの方法を使用してテーブルビューのデータソースプロトコルを実装するデータソース
self.dataSource = [dict objectForKey:@"New item"]; //only if it's guaranteed that the dictionary contains that array
次に辞書から配列を抽出における
委任とプロトコルについて知る必要があります。詳細については、UITableView programming guideをお読みください。