0

我正面临一个关于扩展和缩小 UITableViewCell 的疯狂问题。当我只单击一个单元格时,它可以正常工作,但是当一个单元格保持打开状态时,单击另一个单元格打开会导致单元格紊乱。我想同时扩大和缩小。

这是我的来源。

- (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

问题出在

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

或者可能是问题是由于释放。任何东西都会发布,而您仍在使用它。

第一条评论//所有发布并解释您的

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

于 2012-04-06T12:58:45.280 回答
0

问题来自这样一个事实,即您的单元格在选择时将具有 160.0 的高度。

看这里

v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];

您为 y 原点为 44 的视图提供 116 的高度。因此,您的单元格视图的总高度为 160.0

更改返回的值

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

匹配单元格的正确总高度。

于 2012-04-06T14:20:58.257 回答