3

テーブルのセルの選択時に新しいセルを追加する必要があります。新しいセルは、選択したセルの下に来るはずです。私たちはそれを行うことができますか?

以下は、4 つのセルを作成し、2 と 3 の間にセルを挿入するコードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];


if(indexPath.row == 0){

    cell.textLabel.text = @"My Tweets";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"tweets.png"];
}

if(indexPath.row == 1){

    cell.textLabel.text = @"Search";
    cell.backgroundColor = [UIColor blackColor];

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:@"search.png"];
}

if(indexPath.row == 2){

    cell.textLabel.text = @"Followers";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"followers.png"];
}

if(indexPath.row == 3){

    cell.textLabel.text = @"Following";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"following.png"];
}



return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//here I have to insert a new cell below on the click this existing cell.


if(indexPath.row == 1)
{

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:newIndexPath]; 

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPaths] withRowAnimation:UITableViewRowAnimationAutomatic];
}

}

4

3 に答える 3

1

データ ソースを更新してから、この UITableView メソッドを呼び出す必要があります。

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

これは、

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

テーブル ビュー データ ソースのメソッドを使用して、選択したアニメーションでテーブルを更新します。

投稿したコードで行っているように、didSelectRowAtIndexPath メソッドで新しいセルを作成することはありません。


情報を追加するために編集されました。

(あなたの場合)indexPaths配列を設定するには、これを行うことができます:

NSInteger row = [indexPath row];
NSInteger section = [indexPath section];    
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row+1 inSection:section];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 
于 2012-05-12T13:24:13.543 に答える
1

begin update ブロックと end update ブロック内で insertrowsatindexpath デリゲートを呼び出すだけです。たとえば、次のようにしてみてください。

[tableViewForFinishRace beginUpdates];
        NSIndexPath *newIndexPath = [NSIndexPath    indexPathForRow:arrayForTitle.count-1 inSection:0];
        NSLog(@"index path for insertion =%@",newIndexPath);
        [tableViewForFinishRace insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [arrayForTitle insertObject:@"Gearing Up for a" atIndex:arrayForTitle.count-1];
        [arrayForImage insertObject:@"gearing-up-icon.jpeg" atIndex:arrayForTitle.count-2];
[tableViewForFinishRace endUpdates];
于 2015-04-09T04:24:10.330 に答える
1

あなたの質問に関しては、indexpathに配列を作成しているときにindexpath.row + 1を実行しないでください。次に、tableviewでinsertrowatindexpathを呼び出します....また、テーブルをリロードすると完了です。

このメソッドを追加して、テーブルビューに行を追加できます

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];

    NSArray *paths = [NSArray arrayWithObject:
                            [NSIndexPath indexPathForRow:1 inSection:0]];
        if (editing)
        {
            [[self tableView] insertRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
        else {
           //Your Operation [[self tableView] deleteRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
[tableView reloadData];
    }

UITableview をサブクラス化している場合は、上記の方法を使用できます。それ以外の場合は、チュートリアルを実行します

行を挿入するイベントについても言及します。行を挿入するだけで、次のロジックを実装できます。

パスの配列を作成します:-

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

行とセクションを挿入する前の行番号を指定する必要があります。次に、テーブルビューでメソッド insertRowsAtIndexPaths:withRowAnimation: を呼び出して再読み込みします。

于 2012-05-12T14:00:19.517 に答える