0

私は、一般的な問題であると確信しているものを解決しようとしています。

ストーリーボードに接続された UITableViewController があり、2 つのプロトタイプ セルを使用しています。ナビゲーション コントローラーの最初のビューでは、すべてのセルが正しく読み込まれます。私の問題は、その UITableViewController の別のインスタンスをナビゲーション スタックにプッシュしようとしたときに発生します (データは異なります)。

再利用が識別されたセルはストーリーボードの最初のビューにのみ関連付けられており、必ずしもクラス自体には関連付けられていないため、これらのプロトタイプ セルを再利用しnilて から予測可能な応答を取得する機能が失われていdequeueReusableCellWithIdentifier:ます。

スタックの後半でプロトタイプ セルの使用を維持するにはどうすればよいですか? カスタムセルを含むペン先を接続し、それをテーブルビューに入力する唯一の解決策はありますか?

私の表示ロジックは以下の通りです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id toDisplay = [[navPool getObjectsForParent:indexPath.section] objectAtIndex:indexPath.row];

    if ([toDisplay isKindOfClass:[Category class]]) {
        Category *currentCategory = toDisplay;
        static NSString *CellIdentifier = @"Category";
        CategoryCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.textLabel.text = currentCategory.name;
        [cell setCategoryId:currentCategory.categoryId];
        return cell;
    } else {
        if ([toDisplay isKindOfClass:[ListTopic class]]) {
            ListTopic *currentTopic = toDisplay;
            static NSString *CellIdentifier = @"Topic";
            ListTopicCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell.textLabel.text = currentTopic.name;
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if ([cell isKindOfClass:[CategoryCell class]]) {            
        CategoryCell *cell = (CategoryCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        BrowseViewController *newView = [[BrowseViewController alloc] initWithStyle:UITableViewStylePlain andParent:[cell.categoryId intValue]];
        [self.navigationController pushViewController:newView animated:YES];
    } else if ([cell isKindOfClass:[ListTopicCell class]]) {
        // push detail view
    }
}
4

1 に答える 1

0

私は、私が言及したことを正確に実行し、ペン先を外部で作成し、dequeueReusableCellWithIdentifier:呼び出しがオブジェクトを返さない場合はそれらをロードしました。この質問があまり一般的ではないことに驚いています。私の新しいコードは以下のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id toDisplay = [[navPool getObjectsForParent:self.parent] objectAtIndex:indexPath.row];

    if ([toDisplay isKindOfClass:[Category class]]) {

        Category *currentCategory = toDisplay;

        static NSString *CellIdentifier = @"Category";

        CategoryCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!cell) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CategoryCellView" owner:self options:nil];
            cell = (CategoryCell *)[nib objectAtIndex:0];
        }

        cell.categoryName.text = currentCategory.name;
        [cell setCategoryId:currentCategory.categoryId];

        return cell;
    } else {
        if ([toDisplay isKindOfClass:[ListTopic class]]) {

            ListTopic *currentTopic = toDisplay;

            static NSString *CellIdentifier = @"Topic";

            ListTopicCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

            if (!cell) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TopicCellView" owner:self options:nil];
                cell = (ListTopicCell *)[nib objectAtIndex:0];
            }

            cell.textLabel.text = currentTopic.name;

            return cell;
        }
    }

    NSLog(@"Whatchu tryna pass hommie?");
    return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Default"];
}
于 2013-06-14T18:28:47.263 に答える