Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
xcode ios-tableview节标题中的图像_Ios_Objective C_Uitableview_Tableview - Fatal编程技术网

xcode ios-tableview节标题中的图像

xcode ios-tableview节标题中的图像,ios,objective-c,uitableview,tableview,Ios,Objective C,Uitableview,Tableview,这是我的代码: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIImage *myImage = [UIImage imageNamed:@"photo.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage]; imageView.frame

这是我的代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImage *myImage = [UIImage imageNamed:@"photo.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(10,10,60,60);

    if (section == 0){
        return 0;
    }else if (section == 1){
        return 0;
    }else if (section == 2){
        return 0;
    }else if (section == 3){
        return 0;
    }else if (section == 5){
        return imageView;
    }else{
        return 0;
    }
}

因此,图像出现了,但问题是,
imageView.frame
不起作用,照片大小似乎等于页眉高度,如何解决此问题?

在实现
tableView:viewForHeaderInSection:
方法时,还必须实现
tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        return 70;
    } else {
        return 0;
}
您的
viewForHeaderInSection
方法可以做得更好:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        UIImage *myImage = [UIImage imageNamed:@"photo.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
        imageView.frame = CGRectMake(10, 10, 60, 60);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [view addSubview:imageView];

        return view;
    } else {
        return nil;
    }
}

在执行
tableView:viewForHeaderInSection:
方法时,还必须执行
tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        return 70;
    } else {
        return 0;
}
您的
viewForHeaderInSection
方法可以做得更好:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        UIImage *myImage = [UIImage imageNamed:@"photo.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
        imageView.frame = CGRectMake(10, 10, 60, 60);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [view addSubview:imageView];

        return view;
    } else {
        return nil;
    }
}

在执行
tableView:viewForHeaderInSection:
方法时,还必须执行
tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        return 70;
    } else {
        return 0;
}
您的
viewForHeaderInSection
方法可以做得更好:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        UIImage *myImage = [UIImage imageNamed:@"photo.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
        imageView.frame = CGRectMake(10, 10, 60, 60);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [view addSubview:imageView];

        return view;
    } else {
        return nil;
    }
}

在执行
tableView:viewForHeaderInSection:
方法时,还必须执行
tableView:heightForHeaderInSection:
方法以返回所需的收割台高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        return 70;
    } else {
        return 0;
}
您的
viewForHeaderInSection
方法可以做得更好:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 5) {
        UIImage *myImage = [UIImage imageNamed:@"photo.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
        imageView.frame = CGRectMake(10, 10, 60, 60);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        [view addSubview:imageView];

        return view;
    } else {
        return nil;
    }
}

谢谢,很好用。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想添加的内容。谢谢,效果很好。我可以在图像后面添加背景吗?你可以在视图中添加任何你想要的内容。