我需要做同样的概念,让 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,这使它具有外观。
可能还有其他方法可以更好地完成此任务,但这只是我找到的方法。我希望它可以帮助别人。