0

UITableViewCell の拡大と縮小に関するクレイジーな問題に直面しています。1 つのセルだけをクリックすると正常に動作しますが、1 つのセルが開いたままの状態で別のセルをクリックして開くと、セルが乱れます。拡大と縮小を同時にしたい。

これが私の情報源です。

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    //Add items
    [listOfItems addObject:@"1"];
    [listOfItems addObject:@"2"];
    [listOfItems addObject:@"3"];
    [listOfItems addObject:@"4"];
    [listOfItems addObject:@"5"];
    [listOfItems addObject:@"6"];
    [listOfItems addObject:@"7"];
    [listOfItems addObject:@"8"];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UILabel *aLabel;     UILabel *bLabel; UILabel *v1Label;  UILabel *v2Label;; UIView *v1;  UIView *v2;  


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSLog(@" cell null");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        aLabel                      = [[[UILabel alloc] initWithFrame:CGRectMake(9.0, 8.0, 50.0, 20.0)] autorelease];
        aLabel.tag                  = 1;
        aLabel.font                 = [UIFont systemFontOfSize:30];
        [cell.contentView addSubview:aLabel];


        v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];
        v1.backgroundColor = [UIColor  redColor];
        v1.tag = 10;
        v1.hidden = YES;
        [cell.contentView addSubview:v1];
        [v1 release];     


        v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 100)];
        v2.backgroundColor = [UIColor  blueColor];
        v2.tag = 11;
        v2.hidden = YES;
        [cell.contentView addSubview:v2];
        [v2 release];     

    }

    else  {
            aLabel =  (UILabel *)[cell.contentView viewWithTag:1];

            v1     =  (UIView *) [cell.contentView viewWithTag:10];          
            v2     =  (UIView *) [cell.contentView viewWithTag:11];     
    }

    aLabel.text = [listOfItems objectAtIndex:indexPath.row];

        if (SelectedIndexPath == indexPath.row) 
        {       
            if ([aLabel.text intValue] %  2) {
                v1.hidden = NO;
                v2.hidden = YES;
            }
            else  {
                v1.hidden = YES;
                v2.hidden = NO;
            }
        }
        else {
            v1.hidden = YES;
            v2.hidden = YES;
            }

    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (SelectedIndexPath == indexPath.row)  
    {
        return 100.0;
    }
    else {
        return 46.0;
    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (SelectedIndexPath == -1)
    {
        OldSelectedIndexPath = indexPath.row;
        SelectedIndexPath = indexPath.row;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
    }
    else 
    {
        if (SelectedIndexPath == indexPath.row) 
        {
            OldSelectedIndexPath = SelectedIndexPath;
            SelectedIndexPath = -1;

            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];

        }
        else 
        {
            SelectedIndexPath = indexPath.row;

            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:OldSelectedIndexPath inSection:indexPath.section], indexPath, nil] withRowAnimation:NO];

            OldSelectedIndexPath = SelectedIndexPath;
        }
    }
}
4

2 に答える 2

0

問題は、選択したときにセルの高さが 160.0 になるという事実から生じます。

ここを見て

v1 = [[UIView 割り当て] initWithFrame:CGRectMake(0, 44, 320, 116)];

y 原点が 44 のビューに 116 の高さを指定します。したがって、セル ビューの高さの合計は 160.0 になります。

で返される値を変更します

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

セルの正しい合計の高さに合わせます。

于 2012-04-06T14:20:58.257 に答える
0

問題は

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

または、リリースが原因で問題が発生している可能性があります。何かがリリースされ、あなたはまだそれを使用しています。

最初のコメント // すべてのリリースと解釈

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

于 2012-04-06T12:58:45.280 に答える