Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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中的sectionHeaderView背景图像_Ios_Objective C - Fatal编程技术网

iOS中的sectionHeaderView背景图像

iOS中的sectionHeaderView背景图像,ios,objective-c,Ios,Objective C,我正在尝试在ios应用程序的sectionHeaderView中获取背景图像。有些东西不对,我需要释放它,因为我有两个值 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIImage *myImage = [UIImage imageNamed:@"bar-two.jpg"]; UIImageView *imageView = [[UIImage

我正在尝试在ios应用程序的sectionHeaderView中获取背景图像。有些东西不对,我需要释放它,因为我有两个值

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImage *myImage = [UIImage imageNamed:@"bar-two.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(10,10,1,30);

    return imageView;

    APLSectionHeaderView *sectionHeaderView = [self.tblView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    APLSectionInfo *sectionInfo = (self.sectionInfoArray)[section];
    sectionInfo.headerView = sectionHeaderView;
    sectionHeaderView.titleLabel.text = sectionInfo.climb.name;
    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;

    return  sectionHeaderView;
}

除了@shivan raptor提到的事实,它永远不会达到你代码的一半(在返回imageView;之后,您通过将其分配给您的sectionInfo.headerView来创建对sectionHeaderView的强引用。通过这种方式,即使您的节头被解除分配,sectionHeaderView仍然具有强引用并且永远不会被释放(直到sectionInfoArray被释放为止)。这有潜在的危险,而且您永远不会检查sectionInfo是否有headerView!如果要将其分配给sectionInfo,至少在重新分配之前检查它! 另外,如果您正确初始化sectionHeaderView并将其分配给sectionInfo的headerView,这是更好的做法

        if(!sectionInfo.headerView){

          [initialize sectionHeaderView ...]

          sectionInfo.headerView=sectionHeaderView;

        }

       return sectionInfo.headerView;

我想您需要
基于节的标题
,可以这样尝试:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if(section == 0) {
    UIImage *myImage = [UIImage imageNamed:@"bar-two.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(10,10,1,30);

    return imageView;
    }
    else if (section ==1) {
    APLSectionHeaderView *sectionHeaderView = [self.tblView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    APLSectionInfo *sectionInfo = (self.sectionInfoArray)[section];
    sectionInfo.headerView = sectionHeaderView;
    sectionHeaderView.titleLabel.text = sectionInfo.climb.name;
    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;

    return  sectionHeaderView; 
    }

    NSAssert (FALSE, @"Undefined header requested");

    return nil;
}

什么是不对的?为什么两个返回,你会忘记如果…否则…?一半的代码是无法访问的,因为
返回imageView
lineimageView.frame=CGRectMake(10,10,1,30);您已将imageview宽度保持为1像素宽度是否正确?抱歉,我想我的问题是如何在此代码中将背景图像添加到sectionHeaderView?因此,我的实际问题是如何将图像添加到sectionHeaderView而不干扰我的上述代码。我只想添加背景图像,但我需要其他代码,返回部分HeaderView;仍为空there@user1155141我上面提到的代码是针对您希望引用sectionHeaderView的部分的,因为让我们面对它,您正在设置它的委托,如果没有强引用,您将在返回后立即释放它……您需要创建UIView,添加任何子维ws-like image view,labels,…返回给它。谢谢,我很感谢您的帮助,但现在我只在一个单元格中获得背景图像,在另一个单元格中获得标签。我需要显示标签,并且在所有单元格中显示背景。为标题创建自定义UIView,在其中放置UIImageView。通过代码在其中设置图像,并设置信息如果有必要的话,请查看。因此它将涵盖这两个方面。非常感谢,这就是问题所在。我实际上刚刚将UIImage视图放置在xib中的UIView中,它将其放置得非常完美。再次感谢