web-dev-qa-db-ja.com

viewcontroller内でTableViewを使用する方法は?

ストーリーボードで、View Controllerにテーブルビューを追加しました。Ctrlキーを押しながらTableViewをViewControllerにドラッグし、「デリゲート」と「データソース」を接続しました。 (.h)ファイルに<UITableViewDataSource,UITableViewDelegate>を追加しましたが、アプリを実行するとSIGABRTエラー(?)が発生し、アプリがクラッシュします。私は何をすべきか?

12
b3rge

これまでのところ、実装ファイルにUITableViewDataSourceとUITableViewDelegateを実装する必要があります。

必要な機能は次のとおりです。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [regions count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.
    Region *region = [regions objectAtIndex:section];
    return [region.timeZoneWrappers count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // The header for the section is the region name -- get this from the region at the section index.
    Region *region = [regions objectAtIndex:section];
    return [region name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier]];
    }
    Region *region = [regions objectAtIndex:indexPath.section];
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZoneWrapper.localeName;
    return cell;
}

ここにAppleドキュメント のリンクがあります

20
Levent Yıldız