所以我的 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/