2011-09-07 84 views
0

我無法弄清楚這裏發生了什麼以及爲什麼。我確定了一個我想要改變顏色的特定索引。而且裏面:UITableView,通過使用indexpath.row == ...顏色特定單元格的文本,顏色超過一個單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

我運行以下命令:

if ([savedPRIndex intValue] == indexPath.row) { 
     [customCell togglePR:TRUE]; 
     NSLog(@"TRUE"); 
    }else{ 
     [customCell togglePR:FALSE]; 
    } 

在自定義單元格

- (void)layoutSubviews{ 

    [super layoutSubviews]; 
    CGFloat xPosition = 20.0f; // Default text position 

    if(prToggle){ 
     xPosition = -20.0f; 
     cellText.textColor = [UIColor colorWithRed:0x24/255.0 green:0x9e/255.0 blue:0xd6/255.0 alpha:1.0];  
     UIImage* img = [UIImage imageNamed:@"pr_icon.png"]; 
     CGRect rect = CGRectMake(272.0f, 14.0f, img.size.width/2, img.size.height/2); 
     UIImageView* imgView = [[UIImageView alloc] initWithFrame:rect]; 
     [imgView setImage:img]; 

     [self addSubview:imgView]; 
     [imgView release]; 
    } 

    CGRect textLabelFrame = self.cellText.frame; 
    textLabelFrame.origin.x = xPosition; 
    self.cellText.frame = textLabelFrame; 
} 

-(void)togglePR:(BOOL)TF{ 
    prToggle = TF; 
} 

自定義單元格togglePR就是文本可以改變顏色的唯一的地方,人有有任何想法嗎?

我可以發佈更多的代碼,如果它可以幫助解密發生了什麼事情。

附加代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *customCell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (customCell == nil) { 
     customCell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSLog(@"savedPRIndex : %@", savedPRIndex); 
    NSLog(@"Index Path : %i", indexPath.row); 

    [customCell togglePR:[savedPRIndex intValue] == indexPath.row]; 

    // Configure the cell... 
    customCell.cellText.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Value"]; 
    customCell.cellPower.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"PowerValue"]; 
    customCell.cellSplit.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Split"]; 

    return customCell; 
} 
+0

細胞可以重複使用。您也不應該在'-layoutSubviews'中添加視圖,因爲這可以在視圖的生命週期中多次調用。表格單元格已經有一個圖像視圖,所以你可能應該使用它。 –

回答

1

去向你的第二個if語句?如果是在最初創建單元格時,這可能會導致在單元格被重用時設置多個單元格。你的cellForRowAtIndexPath應具有以下結構:

  • 離隊細胞
  • 檢查是否返回一個單元格對象,初始化一個新的,如果不是
  • 單元配置(無論是取出的細胞或新創建的細胞)

我的猜測是,上面的代碼是在第2部分,而不是部分3.

編輯:剛剛看到你更新的代碼。一旦你設置了切換,你似乎沒有一個機制來解除它 - 例如刪除創建的子視圖等等。您可能會使用切換設置將某個單元格取出,但將其設置爲NO不起作用。

+0

只有當我開始使用'[tableview reloadData];似乎是一個問題;'它讓你感到困惑 –

+0

你認爲當切換設置爲NO時,切換設置爲YES的單元格會發生什麼?之前添加的圖像視圖仍然存在,不是嗎? – jrturton

0

您可能沒有向我們展示足夠的代碼,並在嘗試將單元出列時檢查if(!cell)檢查。這意味着它只能在最初創建單元時運行,而不能用於出隊單元。

現在旁註,在Objective-C使用YESNO,而不是TRUEFALSE,當你需要設置一個布爾值,也沒有必要使用一個,如果檢查,因爲布爾表達式將工作。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableview dequeue...:@"blah"]; 
    if(!cell) 
    { 
     //Create cell 
     //Do not set the colors here 
    } 

    //Set the colors here (no need for an if check) 
    [customCell togglePR:[savedPRIndex intValue] == indexPath.row]; 
} 

編輯:

根據您的更新,你應該叫- (void)setNeedsLayout- (void)setNeedsDisplay當你設置PR標誌。

-(void)togglePR:(BOOL)TF{ 
    prToggle = TF; 
    [self setNeedsDisplay]; 
} 
+0

上面更新的代碼,如果你可以看看。 –

+0

當您更改PR標誌時,請嘗試調用setNeedsLayout或setNeedsDisplay – Joe

+0

沒有運氣,這些調用會做什麼? –