ios - 增加 iOS 中标题和表格单元格之间的空间

标签 ios iphone uitableview cocoa-touch uikit

enter image description here

所以我的 UITableView 有一个标题,也就是显示的 UIImageView,以及图像下方的注释。我正在尝试增加图像和评论表之间的空间。

(我试过增加标题的高度,但在我的情况下它不起作用,因为它会导致更大的 UIImageView 并且图像不会覆盖 View 完全)

我试验过这个技巧:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CommentsTableCell";
    CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Comment *comment = [self.comments objectAtIndex:indexPath.row];
    [cell setUsername:comment.handle andText:comment.text];

    /* Dirty hack: 
     1. We cannot increase the height of the header because that will leave spaces in the image. 
     2. The only way we can increase the margin from the comments table to the picture is by 
     increasing the individual inset of the first and last comments cell 
     */
    if (indexPath.row == 0) {
        [cell setContentInset:UIEdgeInsetsMake(COMMENTS_PADDING * 10 , 0, 0, 0)];
    } else if (indexPath.row == [self.comments count] - 1) {
        [cell setContentInset:UIEdgeInsetsMake(0, 0, COMMENTS_PADDING * 10 , 0)];
    }

    return cell;
}

在我的 CommentsCell.m 中:

- (void)awakeFromNib {
    self.commentText.scrollEnabled = false;
    self.commentText.editable = false;
    self.commentText.contentInset = UIEdgeInsetsMake(-1 * COMMENTS_PADDING, 0, 0, 0);
}

- (void)setUsername:(NSString *)username andText:(NSString *)text {
    [self.commentText setAttributedText:[CommentsCell getContentStringForUsername:username andText:text]];
}

- (void)setContentInset:(UIEdgeInsets)inset {
    self.commentText.contentInset = inset;
}

但第一条评论仍然有相同的插图。我检查了调试器,awakeFromNib 发生在之前 cellForRowAtIndexPath。你明白为什么我的方法不起作用吗?

我也乐于接受其他建议。

最佳答案

您应该能够在您显示的图像正下方的标题 View 中添加一些空间。与其将表格的标题 View 设置为 UIImageView,不如创建一个可以添加 ImageView 的容器 View ,然后在其下方留出一些空间。

- (UIView *) buildTableHeaderView {

    UIImage *image = [UIImage imageNamed: @"my_image.png"];
    CGFloat height = image.size.height + 20;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.frame.size.width, height)];
    [imageView setImage: image];

    UIView *headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.bounds.size.width, height)];
    [headerView addSubview: imageView];

    return headerView;
}

关于ios - 增加 iOS 中标题和表格单元格之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16700329/

相关文章:

ios - Stackmob 不会在没有任何错误的情况下执行提取请求

iphone - CALayer:仅在一侧添加边框

iphone - 苹果如何在没有外部多媒体播放器的情况下批准控制外部多媒体播放器的应用程序?

ios - 从 UITableView 中的 UITextView 更新 DataSource

ios - Objective-C:如何制作自定义水平 ScrollView

ios - UIScrollView 相对于 UITableView 和 UICollectionView 的优势

ios - WatchKit XCODE 6.3 问题

ios - 行点击后调整大小的单元格图像

ios - Twitter 帖子中的自定义 URL 方案不起作用

iphone - 保存在 NSDocumentDirectory 或 NSCachesDirectory