2011-12-23 13 views
-2

最初に、私は配列から項目を数える必要があります。plistの配列を配列し、配列項目のタイトル付きボタンを作成しますか?

<dict> 
    <key>New item</key> 
    <array> 
     <string>apple</string> 
     <string>orange</string> 
    </array> 
</dict> 

第2に、項目をtableview.exampleに、表のセル1をappleで、表のセル2をオレンジでロードします。
3番目に、配列からのカウント項目によって表のセルにボタンを作成します。
第4に、項目value.example、ボタン "apple"、ボタン "orange"でボタンタイトルを設定します。

どこから始めたらよいですか?

答えて

2

まず、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をお読みください。

関連する問題