0

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

//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;
        }
    }
}
4

1 回答 1

1

Move the coloring code outside of the if (cell == nil) { block. Just because a cell was created for an even-numbered index doesn't mean it will only be reused for even ones.

Here's a simple example of code for coloring cells that are being reused:

- (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;
}
于 2012-04-08T19:42:56.277 回答