我正在尝试做一件简单的事情但却遇到了奇怪的问题。我想要做的是根据网络响应设置标签的字体粗体。细胞数量不会超过数百。首先它正确加载,但是当我向上或向下滚动时,在noteStautus
仍然是noteStatus
时,一些标签的字体也会变粗。这是我已经实现的代码。这是表视图数据源的代码。根据{{1}}的值,我正在更改标签的字体大小。我想要的只是noteStatus =0
文本字体应该是粗体,否则是简单的字体。
NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
notificationList = [NSMutableArray new];
NSArray *notificationArray = response[@"Notifications"];
for(NSDictionary *notificationDict in notificationArray)
{
NSDictionary *cellData =@{@"noteStatus":[notificationDict valueForKey:@"status_read"],@"notificationId":[notificationDict valueForKey:@"notification_id"]};
[notificationList addObject:cellData];
}
}
[self.notificationTableview reloadData];`
检查条件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notificationCell" forIndexPath:indexPath];
if (cell==nil)
{
cell = [[NotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"notificationCell"];
}
if([[[notificationList objectAtIndex:indexPath.row]valueForKey:@"noteStatus"] isEqualToString:@"0"])
{
[cell.notificationReviewLabel setFont:[UIFont boldSystemFontOfSize:14]];
cell.notificationReviewLabel.text = [[notificationList objectAtIndex:indexPath.row]valueForKey:@"messageNo"];
cell.dateLabel.text = [[notificationList objectAtIndex:indexPath.row]valueForKey:@"dateNo"];
}
else
{
cell.notificationReviewLabel.text = [[notificationList objectAtIndex:indexPath.row]valueForKey:@"messageNo"];
cell.dateLabel.text = [[notificationList objectAtIndex:indexPath.row]valueForKey:@"dateNo"];
}
return cell;
}
答案 0 :(得分:1)
您应该将 else 语句中的字体重置为
[cell.notificationReviewLabel setFont:[UIFont systemFontOfSize:14]];
您会看到,在重复使用单元格时,它会保存最近的格式。 更好的做法是覆盖NotificationCell的 prepareForReuse ,将所有格式重置为默认值