重新加载行以扩展和收缩时UITableViewCell无序

时间:2012-04-06 12:01:38

标签: iphone uitableview

我正面临着一个关于扩展和缩小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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题在于

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

或者可能是问题是由于释放。什么都会发布,你还在使用它。

首先发表评论//所有发布和填补你的

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

答案 1 :(得分:0)

问题来自于选中时单元格的高度为160.0的事实。

看这里

  

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

对于y原点为44的视图,您的高度为116.因此,您的单元格视图的总高度为160.0

更改

中返回的值
  

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

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