xcode ios - uitableview部分标题中的图像

时间:2014-07-31 23:33:17

标签: 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 = 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无效,照片尺寸看起来与标题高度相等,我该如何解决?

1 个答案:

答案 0 :(得分:3)

实施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;
    }
}