0

3つのテーブルビューと3つのデータソースがあります。XMLからのデータでそれらを埋めます。1つのテーブルビューのみを入力すると、すべてのテーブルビューが同じテキストで入力されます。

すべてのテーブルビューセルには同じ識別子があります:「AlbumCell」

これが私のコードです:

- (void)viewDidLoad
{
    [super viewDidLoad];

    otoFosilArray = [[NSMutableArray alloc] init];

    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    updateArray = appDelegate.derinlikListesiArrayMikro;

    _arrayFosiller1 = [[NSMutableArray alloc] init];
    _arrayFosiller2 = [[NSMutableArray alloc] init];
    _arrayFosiller3 = [[NSMutableArray alloc] init];

    tableFosiller1.dataSource = self;
    tableFosiller1.delegate = self;
    tableFosiller2.dataSource = self;
    tableFosiller2.delegate = self;
    tableFosiller3.dataSource = self;
    tableFosiller3.delegate = self;

    [self otoFosilEkle];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableFosiller1) {
        return 1;
    }
    else if (tableView == self.tableFosiller2) {
        return 1;
    }
    else if (tableView == self.tableFosiller3) {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (cbvMBentik.checked == YES) {
        return [_arrayFosiller1 count];
    }
    if (cbvMPlanktonik.checked == YES) {
        return [_arrayFosiller2 count];
    }
    if (cbvMDiger.checked == YES) {
        return [_arrayFosiller3 count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"AlbumCell"];
    if (cbvMBentik.checked == YES)
        cell.textLabel.text = [_arrayFosiller1 objectAtIndex:indexPath.row];
    if (cbvMPlanktonik.checked == YES)
        cell.textLabel.text = [_arrayFosiller2 objectAtIndex:indexPath.row];
    if (cbvMDiger.checked == YES)
        cell.textLabel.text = [_arrayFosiller3 objectAtIndex:indexPath.row];

    return cell;
}

- (void)otoFosilEkle
{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    otoFosilArray = appDelegate.derinlikListesiArrayMikroFosil;

    NSString *tempStr;

    for (int i = 0; i < [otoFosilArray count]; i++)
    {
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Bentik"]) {

            cbvMBentik.checked = YES;
            cbvMPlanktonik.checked = NO;
            cbvMDiger.checked = NO;
            tableFosiller1.userInteractionEnabled = YES;
            tableFosiller2.userInteractionEnabled = NO;
            tableFosiller3.userInteractionEnabled = NO;

            tempStr = @"text1";

            [_arrayFosiller1 addObject:tempStr];
            [tableFosiller1 reloadData];
        }
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Planktonik"]) {

            cbvMBentik.checked = NO;
            cbvMPlanktonik.checked = YES;
            cbvMDiger.checked = NO;
            tableFosiller1.userInteractionEnabled = NO;
            tableFosiller2.userInteractionEnabled = YES;
            tableFosiller3.userInteractionEnabled = NO;

            tempStr = @"text2";

            [_arrayFosiller2 addObject:tempStr];
            [tableFosiller2 reloadData];
        }
        if ([[[otoFosilArray objectAtIndex:i] Durum]  isEqualToString:@"Diğer"]) {

            cbvMBentik.checked = NO;
            cbvMPlanktonik.checked = NO;
            cbvMDiger.checked = YES;
            tableFosiller1.userInteractionEnabled = NO;
            tableFosiller2.userInteractionEnabled = NO;
            tableFosiller3.userInteractionEnabled = YES;

            tempStr = @"text3"

            [_arrayFosiller3 addObject:tempStr];
            [tableFosiller3 reloadData];
        }
    }
}
4

5 に答える 5

3

あなたはチェックする必要があります

if (tableView == self.tableFosiller1) {

}
else if (tableView == self.tableFosiller2) {

}
else if (tableView == self.tableFosiller3) {

}

同じ識別子を持っているので、すべてのデリゲートメソッドで

于 2013-03-26T09:00:09.620 に答える
3

各テーブルビューにタグを設定してから、switchステートメントでこれらのタグをチェックして、使用しているテーブルビューを判別する必要があります。

- (void)viewDidLoad
{
    ...
    self.tableFosiller1.tag = 1;
    self.tableFosiller2.tag = 2;
    self.tableFosiller3.tag = 3;
    ...
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger num = 0;
    switch(tableView.tag)
    {
       case 1:
       case 2:
       case 3:
         num = 1;
         break;
    }
    return num;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num = 0;
    switch(tableView.tag)
    {
       case 1:
         num = [_arrayFosiller1 count];
         break;
       case 2:
         num = [_arrayFosiller2 count];
         break;
       case 3:
         num = [_arrayFosiller3 count];
         break;
    }
    return num;    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = nil;
    switch(tableView.tag)
    {
       case 1:
         cellText = [_arrayFosiller1 objectAtIndex:indexPath.row];
         break;
       case 2:
         cellText = [_arrayFosiller2 objectAtIndex:indexPath.row];
         break;
       case 3:
         cellText = [_arrayFosiller3 objectAtIndex:indexPath.row];
         break;
    }

    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"AlbumCell"];
    if(!cell)
       cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"AlbumCell"];
    cell.textLabel.text = cellText;

    return cell;
}

ご覧のとおり、複数のテーブルビューが関係していると、状況が非常に複雑になる可能性があります。これに完全に慣れていない場合は、無料のSensibleTableViewフレームワークを使用することをお勧めします。フレームワークは、配列を取得してテーブルビューを生成するだけで機能します。上記のコードと比較すると、本当に簡単です。

于 2013-03-26T09:13:14.030 に答える
1

すべてのデリゲートメソッド、次の条件を確認する必要があります

if (tableView == table1) {

}
else if (tableView == table2) {

}
else if (tableView == table3) {

}
于 2013-03-26T09:04:04.040 に答える
1

に3種類の再利用可能なセルが必要です

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

このメソッドでは、3つの異なるセルタイプの3つの識別子とコードを作成します。

于 2013-03-26T09:05:24.670 に答える
0
if([tableView isEqual:self.tableFosiller1]){

  }
else if([tablView isEqual:self.tableFosiller2]){

   }
else if([tableView isEqual:self.tableFosiller3]){

    }

すべてのtableViewデリゲートメソッドをチェックするだけです

于 2013-03-26T09:07:39.290 に答える