2

私のアプリには UITableView があり、2 つの異なるカスタム UITableViewCells があります。UITableView は、最初に 1 種類のカスタム UITableViewCell でロードされます。私のUITableViewでセルの1つが選択されている場合、選択されたセルを他のタイプのカスタムUITableViewCellで変更したいと思います。これは可能ですか?あなたが得たものを聞かせてください。

ありがとう、NSNolan

#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if(indexPath.row == [self currentCellIndex]) {
        [self setCurrentCellIndex:-1];

        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];

        OldCell *cell = (OldCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell = [nibs objectAtIndex:0];
    }
    else {
        [self setCurrentCellIndex:indexPath.row];

        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];

        NewCell *cell = (NewCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell = [nibs objectAtIndex:0];
    }        
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"OldCell";

    OldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"OldCell" owner:self options:nil];
        cell = [nibs objectAtIndex:0];
    }

    return cell;
}
4

1 に答える 1

6

didSelectRow...メソッドでは、セルの新しい「モード」を示すフラグ (インスタンス変数に格納されている) を更新する必要があります。tableView reloadRowsAtIndexPaths:withRowAnimation:次に、選択した indexPath を渡して呼び出すことにより、行をリロードします。

cellForRow...メソッドでは、フラグを使用して、2 つのタイプのセルのどちらを行に使用するかを決定します。

于 2013-03-09T04:44:22.320 に答える