UITableViewCell行乱

时间:2012-04-08 19:29:26

标签: iphone uitableview

我之前用相同的源代码发布了一些问题。我刚刚发现了一些其他奇怪的东西。事情是,如果我定义重用单元格标识符,每行颜色都很奇怪 但如果我不使用重用标识符,它的工作 请给我任何提示为什么每行颜色不能保持订单。

//its working
static NSString *CellIdentifier = @"Cell";
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];


//it does not work.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; -- does not working.  
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];




- (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];
        cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
        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  whiteColor];
        v1.tag = 10;
        v1.hidden = YES;
        [cell.contentView addSubview:v1];
        [v1 release];     

        UILabel *a = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
        a.text = @"v1.test1";
        [v1 addSubview:a];

        UILabel *b = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
        b.text = @"v1.test2";
        [v1 addSubview:b];


        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];     

        UILabel *c = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
        c.text = @"v2.test1";
        [v2 addSubview:c];

        UILabel *d = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
        d.text = @"v2.test2";
        [v2 addSubview:d];


    }

    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 162.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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将着色代码移到if (cell == nil) {块之外。仅仅因为为偶数索引创建了一个单元格并不意味着它只会被重用于偶数索引。

这是一个简单的代码,用于着色正在重用的单元格:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = @"Anything";
    cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
    return cell;
}