如何实现UITableViewCell这样的边距(pic)?

时间:2014-01-24 23:17:23

标签: ios objective-c user-interface uitableview

我非常喜欢在此应用中将填充添加到UITableViewCell的方式。我怀疑在Storyboard中是否可行,但实现这一目标的最佳方式是什么?

enter image description here

3 个答案:

答案 0 :(得分:2)

我认为你可以在故事板中轻松做到这一点。

只需添加自定义单元格,灰色背景。 在那里添加UIView作为子视图,具有白色背景,并排列大小,使其成为单元格内的较小矩形,因此它获得此边距效果。

然后在该白色子视图中添加标签/图像视图,您就会感觉良好。

这种做法有什么问题吗? 感觉有点像作弊,但为什么不呢?

答案 1 :(得分:0)

使用Storyboard这可能是在iOS 7上使用UITableView实现的令人生畏的布局。通过编程,它可以相当轻松地完成,并且有点耐心。

无论如何,考虑到单元格中的信息量,我很想使用带有UICollectionView的{​​{1}}而不是表格。这可能会让您更轻松,因为您可以在Storyboard中设置单元格大小,区域边距,项目之间的最小距离等。

答案 2 :(得分:0)

您只需要制作自定义表格单元格并添加特定背景

@implementation CustomTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        <init your custom cell here> 

        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
        CGRect whiteRect = CGRectInset(bgView.bounds, 10, 5);
        UIView *innerView = [[UIView alloc] initWithFrame:whiteRect];
        innerView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        innerView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:innerView];
        bgView.backgroundColor = [UIColor colorWithRed:225.0/255 green:224.0/255 blue:230.0/255 alpha:1.0];
        self.backgroundView = bgView;

    }

    return self;
}

此外,您可以以相同的方式添加self.selectedBackgroundView = selectedView,以便在突出显示/选择特定视图时将其分配给您的单元格。

enter image description here

相关问题