Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
增加iOS中标题和表格单元格之间的空间_Ios_Iphone_Uitableview_Cocoa Touch_Uikit - Fatal编程技术网

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

增加iOS中标题和表格单元格之间的空间,ios,iphone,uitableview,cocoa-touch,uikit,Ios,Iphone,Uitableview,Cocoa Touch,Uikit,因此,我的UITableView有一个标题,即所示的UIImageView,以及图像下方的注释。我试图增加图像和注释表之间的空间 (我已尝试增加标题的高度,但在我的情况下不起作用,因为这将导致更大的UIImageView,并且图像无法完全覆盖视图) 我尝试过这种方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static

因此,我的
UITableView
有一个标题,即所示的
UIImageView
,以及图像下方的注释。我试图增加图像和注释表之间的空间

(我已尝试增加标题的高度,但在我的情况下不起作用,因为这将导致更大的
UIImageView
,并且图像无法完全覆盖视图)

我尝试过这种方法:

- (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;
}
在我的评论中

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


我也愿意接受其他建议

您可以做的是创建一个自定义UIView(如果您想要更简单的UI设计,可以使用.xib),底部有一个空格,然后从
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
方法的
UITableViewDelegate
返回它,另外,不要忘记通过实现相同的
UITableViewDelegate
方法的
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
返回页眉视图的高度

下面是一个简短的例子:

-UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YourCustomHeaderView *headerView = [YourCustomHeaderView instantiateView]; //static method (you can rename it) that will load the custom view from a .xib
     //do aditional UI setup or not
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
      return DEFAULT_HEADER_HEIGHT; //a defined value 
}

如果您使用的是单个标题视图,那么您应该使用相同的自定义标题视图创建/init/setup,但在is superview中向下移动表格,并在顶部的任意位置添加自定义标题视图。

您可以做的是创建一个自定义UIView(如果希望更简单的UI设计,请使用.xib)底部有一个空格,并从
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
方法的
UITableViewDelegate
返回,也不要忘记通过实现
-(CGFloat)tableView:(UITableView*)返回页眉视图的高度表头的表视图高度section:(NSInteger)section
方法相同
UITableViewDelegate

下面是一个简短的例子:

-UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YourCustomHeaderView *headerView = [YourCustomHeaderView instantiateView]; //static method (you can rename it) that will load the custom view from a .xib
     //do aditional UI setup or not
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
      return DEFAULT_HEADER_HEIGHT; //a defined value 
}

如果只有一个标题视图,则应使用相同的自定义标题视图创建/init/setup,但在is superview中向下移动表格,并在顶部任意位置添加自定义标题视图。

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

- (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;
}

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

- (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;
}