UITableView,通过使用indexpath.row == ...颜色特定单元格的文本,颜色超过一个单元格
我无法弄清楚这里发生了什么以及为什么。我确定了一个我想要改变颜色的特定索引。而且里面: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;
}
去向你的第二个if语句?如果是在最初创建单元格时,这可能会导致在单元格被重用时设置多个单元格。你的cellForRowAtIndexPath应具有以下结构:
- 离队细胞
- 检查是否返回一个单元格对象,初始化一个新的,如果不是
- 单元配置(无论是取出的细胞或新创建的细胞)
我的猜测是,上面的代码是在第2部分,而不是部分3.
编辑:刚刚看到你更新的代码。一旦你设置了切换,你似乎没有一个机制来解除它 - 例如删除创建的子视图等等。您可能会使用切换设置将某个单元格取出,但将其设置为NO不起作用。
只有当我开始使用'[tableview reloadData];似乎是一个问题;'它让你感到困惑 –
你认为当切换设置为NO时,切换设置为YES的单元格会发生什么?之前添加的图像视图仍然存在,不是吗? – jrturton
您可能没有向我们展示足够的代码,并在尝试将单元出列时检查if(!cell)
检查。这意味着它只能在最初创建单元时运行,而不能用于出队单元。
现在旁注,在Objective-C使用YES
和NO
,而不是TRUE
和FALSE
,当你需要设置一个布尔值,也没有必要使用一个,如果检查,因为布尔表达式将工作。
- (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];
}
细胞可以重复使用。您也不应该在'-layoutSubviews'中添加视图,因为这可以在视图的生命周期中多次调用。表格单元格已经有一个图像视图,所以你可能应该使用它。 –