4

そのため、1 つのビューで 2 つのテーブルビューを作成しようとしていますが、問題が発生しています。それを行う方法について他の回答を読みましたが、正確には役に立ちません。

私の .h ファイルでは、それらmyFirstViewTextを呼び出す 2 つのビュー用に 2 つのアウトレットを作成し、mySecondViewTex

だから私のmファイルでは -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

異なるコントローラーごとに個別の値を出力できるようにしたいのですが、セルが 1 つしか返されないのでよくわかりません。

これまでのところ、私はこれをやった

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Rx";
static NSString *CellIdentifier2 = @"allergies";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2];
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (!cell2) {
    cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
if (tableView == self.myFirstTextView ) {
       cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
if (tableView == self.mySecondTextView) {
      cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
tableView = self.mySecondTextView;
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;

これにより、最初の TableView に「I love Jazzy」が出力され、2 番目の TableView には何も出力されません。なぜこれが起こり、どうすれば修正できますか? ありがとう

4

4 に答える 4

7

このメソッドは、クラスのインスタンスが dataSource として設定されているすべての tableView によって呼び出されます。

これは、どのtableView がセル番号などを要求していたかを確認する必要があることを意味します。

したがって、メソッドは基本的に次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == rxTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    else if (tableView == allergiesTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    return nil; //because you don't expect any other tableView to call this method
}
于 2012-11-18T14:21:05.897 に答える
4

私の提案は、両方のテーブルビューにタグを設定してから、テーブルビューの dataSource メソッドで、テーブルビューがどのタグにあるかを確認することです。たとえば、コードのどこで実行できるか:

myFirstTableView.tag = 1;
mySecondTableView.tag = 2;

後であなたがいるとき

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.tag == 1) 
   cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];

   if (tableView.tag == 2) 
   cell.textLabel.text = @"BYE JAZZY";
 }

また、サイズの異なる 2 つのテーブルビューがある場合は、次のように実現できます。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    if (tableView.tag == 1)
         return 1;
    else if (tableView.tag == 2)
         return 2;
  }
于 2012-11-19T04:52:34.557 に答える
1

あまり実装されていないアプローチの 1 つは、実際には NSObject サブクラスを使用して、ビュー コントローラーではなく、各テーブルのデリゲートおよびデータソースとして機能することです。「if (tableView == someTable) ...」コードの必要性がなくなり、View Controller と UITableView コードの両方が保守可能になり、読みやすくなる可能性があります。

テーブルごとに NSObject サブクラスを作成します。これらのサブクラスには、UITableView データソースとデリゲート メソッドの目的の実装が含まれます。次に、View Controller でそれぞれを 1 つインスタンス化し、それぞれを適切なテーブルに接続します。

于 2012-11-19T04:41:43.803 に答える