section Header在iOS中查看背景图片

时间:2014-02-24 06:21:27

标签: ios objective-c

我正在尝试在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;
}

2 个答案:

答案 0 :(得分:0)

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

        if(!sectionInfo.headerView){

          [initialize sectionHeaderView ...]

          sectionInfo.headerView=sectionHeaderView;

        }

       return sectionInfo.headerView;

答案 1 :(得分:0)

我想你想要section based header,试试这样:

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