-1

同じクラスに 2 つのテーブルがあり、各テーブルに異なるデータが含まれている必要がありますが、デリゲートに問題があります... 各テーブルに個別のデリゲートを含めるにはどうすればよいですか? ありがとう、私の英語でごめんなさい。

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [dataTable1 count];
     }

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


    CeldaFamilia *cell = (CeldaFamilia *)[aTableView dequeueReusableCellWithIdentifier:@"CeldaFamilia"];




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

    }

    cell.propTextFamilia.text =[dataTable1 objectAtIndex:indexPath.row];

    return cell;
     }
4

3 に答える 3

3

これを行うには、tableView渡された引数を確認します。例:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView1) {
        return [dataTable1 count];
    } else /* tableView == self.tableView2 */ {
        return [dataTable2 count];
    }
}

このパターンでは、すべてのandメソッドにifステートメントを入れる必要があります。UITableViewDataSourceUITableViewDelegate

それを行う別の方法は、テーブル ビューのデータの配列を返す 1 つのメソッドを作成することです。

- (NSArray *)dataTableForTableView:(UITableView *)tableView {
    if (tableView == self.tableView1) {
        return dataTable1;
    } else /* tableView == self.tableView2 */ {
        return dataTable2;
    }
}

次に、各データ ソース/デリゲート メソッドでその関数を使用します。例:

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

各テーブルのデータがどのように見えるかによっては、メソッドにステートメントがtableView:cellForRowAtIndexPath:必要になる場合があります。if

ただし、どちらのパターンも使用しないことをお勧めします。テーブル ビューごとに個別のデータ ソース/デリゲートを作成すると、コードが整理されて理解しやすくなります。必要に応じて、同じクラスの 2 つのインスタンスを使用することも、2 つの異なるクラスを作成して各クラスの 1 つのインスタンスを使用することもできます。

于 2013-08-18T20:18:55.780 に答える