0

コア データを使用してエンティティを取得しています。これらの結果がテーブルビューの 2 番目のセクションに表示され、別のセクションに何かが表示されるようにしたいだけです...アプリはクラッシュしていませんが、フェッチされたデータがテーブルビューに表示されません...私も確信していますデータを正しく取得しています。

ここにいくつかのコードがあります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

if (indexPath.section==0){
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = _team.teamName;
            break;
        case 1:
            cell.textLabel.text = _team.headCoach;
            break;
        default:
            break;
    }

}

if (indexPath.section ==1) {
    Player *p = [_fetchedResultsController objectAtIndexPath: indexPath];
    cell.textLabel.text = p.firstName;
    cell.detailTextLabel.text = p.team.teamName;
}       

return cell;

}
4

1 に答える 1

1

いくつかの問題があります。まず、セクションは 1 つだけにする必要があるため、セクションのプロパティにアクセスする必要はありません。だからこれを試してください

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section){
        case 0:
            return 7;
        case 1:
            return [self.fetchedResultsController.fetchedObjects count];
    }
    return 0;
}

次に、次のコードを使用している場所に問題があります。

Player *p =[_fetchedResultsController objectAtIndexPath: indexPath];

両方のセクションに対して呼び出しており、フェッチにセクションが 1 つしかないため、問題が発生しています。

クラッシュを修正するには、正しい indexPath.section をチェックする条件付きでラップするか、セクション 1 の switch/case ステートメント内に配置します。次のようなことができます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section==0){
        switch (indexPath.row) {
        case 0:
            cell.textLabel.text = _team.teamName;
            break;
        case 1:
            cell.textLabel.text = _team.headCoach;
            break;
        default:
            break;
        }

    }else{
        Player *p = [self.fetchedResultsController.fetchedObjects objectAtIndex: indexPath.row];
        cell.textLabel.text = p.firstName;
        cell.detailTextLabel.text = p.team.teamName;
    }       

    return cell;

}

幸運を

T

于 2012-07-06T16:54:30.093 に答える