Objective-C UITableViewのCellが反転してしまいます。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Mr.K

Objective-C UITableViewのCellが反転してしまいます。

#1

投稿記事 by Mr.K » 9年前

以下のようなコードを書いたのですが、
UITableViewの中にComboBoxを作り、Cellをクリックした際に、3つの新しいCellが作成されるというプログラムです。
ですが、なぜかクリックをするたびに、新しく作成されたCellの順番が反転してしまいます。

本来は
Object 1 ↓

Object 1

Object 2

Object 3

という風にしたいのですが、
クリックし、一度閉じ、再びクリックすると

Object 1 ↓

Object 3

Object 2

Object 1

という状態になってしまいます。
理由がいまいちわからず、なかなか進めることができません。
どうか回答の方をよろしくお願い致します。





コード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil){
        
        switch ([indexPath section]) {
            case 0:{
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
                
                cell.textLabel.text = @"first";
                break;
            }
            case 1:{
                switch ([indexPath row]) {
                    case 0: {
                        
                        DropDownCell *cell = (DropDownCell*) [tableView dequeueReusableCellWithIdentifier:@"DropDownCell"];
                        
                        if (cell == nil){
                            NSLog(@"New Cell Made");
                            
                            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DropDownCell" owner:nil options:nil];
                            
                            for(id currentObject in topLevelObjects)
                            {
                                if([currentObject isKindOfClass:[DropDownCell class]])
                                {
                                    cell = (DropDownCell *)currentObject;
                                    break;
                                }
                            }
                            
                            if (dropDown1Open) {
                                [cell setOpen];
                            }
                            
                            [[cell textLabel] setText:dropDown1];
                        }
                        
                        // Configure the cell.
                        return cell;
                        
                        break;
                    }
                    default: {
                        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
                        
                        if (cell == nil) {
                            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
                        }
                        
                        NSString *label = [NSString stringWithFormat:@"Option %ld", [indexPath row]];
                        
                        [[cell textLabel] setText:label];
                        
                        // Configure the cell.
                        return cell;
                        
                        break;
                    }
                }
                
                break;
            }
                
        }
    }
    return cell;
}


コード:




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath section]) {
        case 1: {
            
            switch ([indexPath row]) {
                case 0:
                {
                    DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath];
                    
                    NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]];
                    NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]];
                    NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]];
                    
                    NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];

                    if ([cell isOpen])
                    {
                        [cell setClosed];
                        dropDown1Open = [cell isOpen];
                        
                        [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
                    }
                    else
                    {
                        [cell setOpen];
                        dropDown1Open = [cell isOpen];

                            [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
   
                    }
                    
                    break;
                }
                default:
                {
                    dropDown1 = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
                    
                    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]];
                    DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path];
                    
                    [[cell textLabel] setText:dropDown1];
                    
                    NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]];
                    NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]];
                    NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]];
                    
                    NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];
                    
                    [cell setClosed];
                    dropDown1Open = [cell isOpen];
                    
                    [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
                    
                    break;
                    
                }
            }
            
        }
            
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


“C言語何でも質問掲示板” へ戻る