2012-04-08 20 views
0

同じソースコードで質問をいくつか投稿しました。私はちょうど他の奇妙なことを見つけました。そのことは、再使用セル識別子を定義すると、各行の色が変わっています。
しかし、私は再使用識別子を使用しない場合、その作業。
各行の色が注文を保持しない理由を教えてください。UITableViewCell行の障害

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

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; 
} 
+0

もしブロックの外側にカラーコードを移動する場合、各row.Nothingが起こるため、それは適用されない:ここ

は再利用されている細胞を着色するためのコードの簡単な例です。私は上で述べた、再利用された識別子のない作業。なぜ再利用された識別子ではうまくいかないのか分かりません。パフォーマンスのために再使用セルを使用する必要があります。 – yongjoon

+0

再利用識別子がないと、常に新しいセルが作成されます。この問題は、セルが無限でないときに発生します...それは、コードが新しいものだったときのセルの色をそのまま使用するときです。 –

+0

それは、私は2行ごとに異なる色を使うことができないことを意味しますか?もう1つの質問は、私は再利用のセル識別子を使用したいと思いますが、セル識別子を定義すると、スクロールアップとスクロールがうまくなり、もう一方は行順がかなり変わってきます – yongjoon