【问题标题】:xcode ios - image in tableview section headerxcode ios - 表格视图部分标题中的图像
【发布时间】:2014-07-31 23:33:17
【问题描述】:

这是我的代码:

- (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 不起作用,照片大小看起来等于标题高度,我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c uitableview tableview


    【解决方案1】:

    当您实现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;
        }
    }
    

    【讨论】:

    • 谢谢,工作正常。我可以在图片后面添加背景吗??
    • 你可以在视图中添加任何你想要的东西。
    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多