7

最初は、追加ボタンが1つしかないテーブルビューがあります。

ユーザーがこのボタンを押すと、次のようにセル数を増やしてセルを追加する必要があります

行数の書き方と追加ボタンをクリックして新しい行を追加する方法

//行数

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return **????** ;
}

//セル/行のコンテンツ

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

//###新しい行を追加..###

-(IBAction)myAction:(id)sender
{
    ???????? ;
}

前もって感謝します...

4

5 に答える 5

11

UITableView行またはセクションを挿入するプロパティがあります。AppleDocを参照してください

これに関する多くのチュートリアルがあります。行/セクションを追加/削除するために一般的に使用されるものが2つあります。

insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:

私はこれを使用する方法について同様の回答を投稿しました: iPhoneのスイッチON/OFFでテーブルビューセルを非表示にする

于 2012-05-02T05:51:28.360 に答える
7

アプリケーションでは、btnでアクションを実行するときに、配列に値を追加する必要があります。

たとえば、テーブルビューでは、cell.textLABEL.textにNSStringを表示しています。これらの文字列はNSMutableArrayにあります。

buttonActionが呼び出されたとき

myAction

{
    NSString *newString =@"NEW CELL";

    [tableArray addObject:newString];
    [tableView reloadData];
}

モーダルに関して、アプリケーションでこのロジックを試してください。

このロジックがお役に立てば幸いです。

于 2012-05-02T06:02:26.110 に答える
7

行を追加または削除するたびにテーブルビューを再ロードすると、アプリケーションのユーザーのエクスペリエンスが低下します。このタスクを実行するのは効率的な方法ではありませんが、いくつかのマイナスの副作用もあります。選択した行はリロード後に選択されたままにならず、変更はアニメーション化されません。

UITableViewテーブルビューのコンテンツを動的に変更するために作成されたメソッドがあります。これらは:

insertRowsAtIndexPaths:withRowAnimation:
moveRowAtIndexPath:toIndexPath:
deleteRowsAtIndexPaths:withRowAnimation:

これらのメソッドを使用すると、指定した操作の実行時に使用されるアニメーションの種類を指定できることに注意してくださいreloadData。テーブルビューのコンテンツを変更するために使用する場合、この種類の動作を実現することはできません。

さらに、テーブルビューの追加のメソッドを使用して、複数のテーブルビュー操作を組み合わせることもできます(これは必須ではありません)。

beginUpdates endUpdates

実行する操作を呼び出しにラップするだけでbeginUpdatesendUpdatesメソッドとテーブルビューは、呼び出しと呼び出しの間で要求されたすべての操作に対して1つのアニメーションを作成するため、遷移全体が、いくつかの別々のアニメーションによって作成されたものよりも見栄えが良くなります。beginUpdatesendUpdates

[self.tableView beginUpdates]
//calls to insert/move and delete methods   
[self.tableView endUpdates]

データソースの状態を。によって保持されている状態と一致させることが非常に重要ですUITableView。このため、テーブルビューが要求された操作の実行を開始すると、そのデータソースが正しい値を返すことを確認する必要があります。

[self.tableView beginUpdates]
//calls to insert/move and delete methods   
//operations on our data source so that its
//state is consistent with state of the table view
[self.tableView endUpdates]

テーブルビューが操作の実行を開始するのはいつですか?beginUpdatesこれは、操作がメソッドによって定義されたアニメーションブロック内にあるかどうかによって異なりendUpdatesます。はいの場合、テーブルビューはendUpdatesメソッド呼び出し後に操作の実行を開始します。それ以外の場合、テーブルビューは、挿入/移動または削除メソッドの呼び出しが行われた直後に操作を実行します。

テーブルビューで操作を実行するメソッドを使用beginUpdatesendUpdatesている場合、この場合、テーブルビューは要求された操作を「バッチ処理」し、テーブルビューで行った呼び出しの順序と同じである必要はない特定の順序で実行することを知っておく必要があります。オブジェクト(このトピックに関するAppleのドキュメント)。

覚えておくべき最も重要なことは、すべての削除操作は常にすべての挿入操作の前に実行されるということです。また、挿入操作(インデックス1、2、3の操作)を昇順で実行すると、削除操作(インデックス3、2、1の操作)が降順で実行されるように見えます。これは、データソースの状態をテーブルビューで保持されている状態と一致させるために重要であることを忘れないでください。

以下の例に示すように、データソースとテーブルビューの操作の順序を分析するために時間を費やしてください。

最後の例:

//initial state of the data source
self.numbers = [@[@(0), @(1), @(2), @(3), @(4), @(5), @(6)] mutableCopy];
//
//...
//

NSArray indexPathsToRemove = @[[NSIndexPath indexPathForRow:3 section:0].
                               [NSIndexPath indexPathForRow:0 section:0];
NSArray indexPathsToAdd = @[[NSIndexPath indexPathForRow:6 section:0],
                            [NSIndexPath indexPathForRow:5 section:0]];

[self.tableView beginUpdates];

[self.numbers removeObjectAtIndex:3];
[self.numbers removeObjectAtIndex:0];

[self.numbers insertObject:@(10) atIndex:4];
[self.numbers insertObject:@(11) atIndex:5];

[self.tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationAutomatic];

[self.tableView endUpdates];
//final state of the data source ('numbers') - 1, 2, 4, 5, 6, 10, 11
于 2014-07-27T18:27:09.113 に答える
2

ここには他にも正しい答えがあります(さらに深く掘り下げているので読む必要があります)が、(私が見つけた!)がなかったために見つけたすべての解決策を読んだ後、私はこれで1週間以上苦労しました包括的な例。

これが機能するためのルール:1。に表示しているアイテムを含む配列に直接変更を加える必要がありますUITableView。メソッドのaの値をの値と等しくなるように設定した場合UITableViewCelltableView:cellForRowAtIndexPath:これらのメソッドが機能するようにself.expandableArray変更を加える必要があります。self.expandableArray

  1. tableViewに表示されるアイテムの配列への変更 [tableView beginUpdates][tableView endUpdates]
  2. indexPaths配列の数は、tableViewに追加する追加アイテムの数と同じである必要があります(これは明らかだと思いますが、指摘しても問題ありません)

これは、それ自体で機能する非常に単純な例です。

    @interface MyTableViewController ()
@property (nonatomic, strong) NSMutableArray *expandableArray;
@property (nonatomic, strong) NSMutableArray *indexPaths;
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation MyTableViewController

- (void)viewDidLoad
{
    [self setupArray];
}

- (void)setupArray
{
    self.expandableArray = @[@"One", @"Two", @"Three", @"Four", @"Five"].mutableCopy;
}

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you should create a cell that displays information from self.expandableArray, and return it
}

//call this method if your button/cell/whatever is tapped
- (void)didTapTriggerToChangeTableView
{
    if (/*some condition occurs that makes you want to expand the tableView*/) {
        [self expandArray]
    }else if (/*some other condition occurs that makes you want to retract the tableView*/){
        [self retractArray]
    }
}

//this example adds 1 item
- (void)expandArray
{
    //create an array of indexPaths
    self.indexPaths = [[NSMutableArray alloc] init];
    for (int i = theFirstIndexWhereYouWantToInsertYourAdditionalCells; i < theTotalNumberOfAdditionalCellsToInsert + theFirstIndexWhereYouWantToInsertYourAdditionalCells; i++) {
        [self.indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates
    [self.myTableView beginUpdates];
    //HERE IS WHERE YOU NEED TO ALTER self.expandableArray to have the additional/new data values, eg:
    [self.expandableArray addObject:@"Six"];
    [self.myTableView insertRowsAtIndexPaths:self.indexPaths withRowAnimation:(UITableViewRowAnimationFade)];  //or a rowAnimation of your choice

    [self.myTableView endUpdates];
}

//this example removes all but the first 3 items
- (void)retractArray
{
    NSRange range;
    range.location = 3;
    range.length = self.expandableArray.count - 3;

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates
    [self.myTableView beginUpdates];
    [self.expandableArray removeObjectsInRange:range];
    [self.myTableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationFade];  //or a rowAnimation of your choice
    [self.myTableView endUpdates];
}

@end

これにより、誰かの時間と頭痛の種を大幅に節約できることを願っています。このようにすると、tableView全体をリロードして更新する必要がなくなり、アニメーションを選択できるようになります。無料のコード、ノックしないでください。

于 2015-09-10T21:20:38.273 に答える
0

配列にオブジェクトを追加し、ボタンをクリックするだけでテーブルビューを再読み込みできます。

[array addobject:@""];
[tableview reloaddata];
于 2012-05-02T06:03:03.240 に答える