Ответ 1
Следующий код тестировался в iOS7 и iOS8 и работает следующим образом:
- Если count > ноль, он отобразит счет в белом тексте с красным значком вокруг него.
- Если count = zero, он отобразит простой индикатор раскрытия.
- (void)updateTableViewCell:(UITableViewCell *)cell forCount:(NSUInteger)count
{
// Count > 0, show count
if (count > 0) {
// Create label
CGFloat fontSize = 14;
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:fontSize];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
// Add count to label and size to fit
label.text = [NSString stringWithFormat:@"%@", @(count)];
[label sizeToFit];
// Adjust frame to be square for single digits or elliptical for numbers > 9
CGRect frame = label.frame;
frame.size.height += (int)(0.4*fontSize);
frame.size.width = (count <= 9) ? frame.size.height : frame.size.width + (int)fontSize;
label.frame = frame;
// Set radius and clip to bounds
label.layer.cornerRadius = frame.size.height/2.0;
label.clipsToBounds = true;
// Show label in accessory view and remove disclosure
cell.accessoryView = label;
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Count = 0, show disclosure
else {
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}