Мне нужно было реализовать ту же концепцию, чтобы UITableCells имели «пробел» между ними. Поскольку вы не можете буквально добавить пространство между ячейками, вы можете имитировать его, манипулируя высотой ячейки UITableView, а затем добавляя UIView в contentView вашей ячейки. Вот скриншот прототипа, который я сделал в другом тестовом проекте, когда симулировал это:

Вот некоторый код (Примечание: в демонстрационных целях есть много жестко закодированных значений)
Во-первых, мне нужно было установить heightForRowAtIndexPath
, чтобы учесть различные высоты на UITableViewCell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.newsArray objectAtIndex:[indexPath row]];
if ([text isEqual:@"December 2012"])
{
return 25.0;
}
return 80.0;
}
Далее я хочу манипулировать внешним видом UITableViewCells, поэтому я делаю это в методе willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
.
- (void)tableView:(UITableView *)tableView willDisplayCell:(NewsUITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.IsMonth)
{
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"month-bar-bkgd.png"];
UILabel *monthTextLabel = [[UILabel alloc] init];
CGFloat font = 11.0f;
monthTextLabel.font = [BVFont HelveticaNeue:&font];
cell.backgroundView = av;
cell.textLabel.font = [BVFont HelveticaNeue:&font];
cell.textLabel.textColor = [BVFont WebGrey];
}
if (indexPath.row != 0)
{
cell.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,70)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
}
Обратите внимание, что я сделал мой whiteRoundedCornerView высотой 70,0, и это то, что вызывает моделируемое пространство, потому что высота ячейки на самом деле 80,0, но мой contentView равен 70,0, что придает ему вид.
Могут быть и другие способы сделать это еще лучше, но я просто нашел, как это сделать. Я надеюсь, что это может помочь кому-то еще.