昨天我问了一个关于单元格没有正确显示取决于字符串值的按钮的问题。如果需要,请看一下:带有核心数据的表格视图的奇怪行为。
用户@jrturton 在他的回答中指出以下内容:
重复使用的单元格每次都会添加子视图 - 因此可能会有许多紧急视图相互叠加。单元格应该只添加一次并将其保存在属性中
我认为这个答案标志着我必须遵循以解决我的问题的正确方向,但我无法将答案实施到我的代码中,如下所示:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *isUrgent = [[managedObject valueForKey:@"urgent"]description];
[[cell textLabel] setText:[[managedObject valueForKey:@"thingName"] description]];
//urgent
if ([isUrgent isEqual:@"Urgent"]){
UIButton *urgentButton = [[UIButton alloc]initWithFrame:CGRectMake(71, 27, 18, 18)];
[urgentButton setImage:[UIImage imageNamed:@"urgent-3"]forState:UIControlStateNormal];
[cell addSubview:urgentButton];
NSLog(isUrgent);
}
//not urgent
if ([isUrgent isEqual:@"Not urgent"]){
UIButton *urgentButton = [[UIButton alloc]initWithFrame:CGRectMake(71, 27, 18, 18)];
[urgentButton setImage:[UIImage imageNamed:nil]forState:UIControlStateNormal];
[cell addSubview:urgentButton];
NSLog(isUrgent);
}
[[cell detailTextLabel] setText:@" "];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:22.0f];
cell.detailTextLabel.font = [UIFont fontWithName:@"Noteworthy" size:15.0f];
}
单元格的行为必须如下:
1. If isUrgent = @"Urgent", the cell must show urgentButton (including imageNamed:@"urgent-3":
2. Else no button has to be shown.
当前行为如下:
1. If isUrgent = @"Urgent", the cell shows urgentButton (including imageNamed:@"urgent-3".
2. If isUrgent = @"Not urgent", value tested in NSLog, the cell shows urgentButton too.
此行为仅在单元格至少更改一次其 isUrgent 值时发生。
我需要您的帮助来实施上述答案。谢谢你。