我無法弄清楚這裏發生了什麼以及爲什麼。我確定了一個我想要改變顏色的特定索引。而且裏面: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;
}
細胞可以重複使用。您也不應該在'-layoutSubviews'中添加視圖,因爲這可以在視圖的生命週期中多次調用。表格單元格已經有一個圖像視圖,所以你可能應該使用它。 –