我有一个tableView,其中列出了学生姓名,并突出显示当前绿色的行或红色表示缺席。如果单击该行,颜色将更改为反映并更改学生的状态。 但是,当学生被标记为缺席(突出显示为红色),然后我将该行滚出视图时,该行变为灰色。我希望这一排保持红色(呃)。 这是tableView方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [kidsNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [kidsNames objectAtIndex:(indexPath.row)];
NSString * status=[kidsAbsent objectAtIndex:indexPath.row];
if([status isEqualToString: @"Present"]==1){
cell.contentView.backgroundColor = [UIColor greenColor];
}else{
cell.contentView.backgroundColor = [UIColor redColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"CLICKED!");
if([ab isEqualToString: @"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(@"Now Absent %@",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Present"];
NSLog(@"Now Present %@",[kidsNames objectAtIndex:(indexPath.row)]);
}
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"CLICKED!");
if([ab isEqualToString: @"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(@"Now Absent %@",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Present"];
NSLog(@"Now Present %@",[kidsNames objectAtIndex:(indexPath.row)]);
}
//send info to the web===============================================================================
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
答案 0 :(得分:0)
尝试设置cell.backgroundView而不是cell.contentView.backgroundColor
或者使用cell.contentView.backgroundColor,但是你应该注意,必须在tableView:willDisplayCell:forRowAtIndexPath:
方法中设置backgroundColor(来自UITableViewCell reference):