我有一个UITableview和UICutsom单元格。在那个UITableview中,我使用Web服务填充数据。我改变了细胞的背景颜色和字体颜色。当我向上和向下滚动Tableview单元格时,背景颜色和标签颜色也会在另一个单元格上更改。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"FieldInventoryCell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
FieldInventoryCell *cell=(FieldInventoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"FieldInventoryCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
NSArray *ar = [[arr objectAtIndex:indexPath.row]componentsSeparatedByString:@"|"];
cell.Status.text=[ar objectAtIndex:1];
cell.serialnumber.text=[ar objectAtIndex:0];
cell.date.text=[ar objectAtIndex:2];
if([cell.serialnumber.text isEqualToString:@"Grand Total"])
{
cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor whiteColor];
}
if ([cell.serialnumber.text isEqualToString:@"INV_FLD Status Total"])
{
//cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor orangeColor];
}
return cell;
}
答案 0 :(得分:0)
据我所知,您可能遇到的问题与出列单元格有关。您正在为contentView和序列号标签设置一些颜色,但如果不满足条件,则不会重置它们。由于表格会重复使用单元格,因此您的已着色项目将显示为新元素。要解决此问题,请将颜色重置为默认值,例如
// ...
if([cell.serialnumber.text isEqualToString:@"Grand Total"])
{
cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor whiteColor];
} else {
// reset color
cell.contentView.backgroundColor=[UIColor clearColor];
cell.serialnumber.textColor=[UIColor blackColor];
}
if ([cell.serialnumber.text isEqualToString:@"INV_FLD Status Total"])
{
//cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor orangeColor];
} else {
// reset color
cell.serialnumber.textColor=[UIColor blackColor];
}