【问题标题】:Changing UITableViewCell width to fit a UIImageView outside of the cell更改 UITableViewCell 宽度以适应单元格外部的 UIImageView
【发布时间】:2012-05-04 16:57:38
【问题描述】:

我想知道从以下布局中实现的最佳方式/正确方式?我想将 UIImageView 放在 UITableViewCell 之外的带有静态单元格的 UITableView 中。


我通过使用以下代码为第 1 节中的单元格子类化 UITableViewCell 来完成以下操作

- (void)setFrame:(CGRect)frame {
    frame.size.width -= 125.0;
    [super setFrame:frame];
}

在 UITableViewController 的 viewDidLoad 中添加 UIImageView 并根据自定义 UITableViewCell 的宽度定位它。

这种工作,但我不知道如何处理轮换以及我到目前为止所做的是否是正确的方法?

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    有不同的方法可以做到这一点。一种是设置表格视图的宽度,如图 2 所示,使用自定义表格视图单元格并在所需的单元格上添加图像,以便显示单元格数据和图像。我认为自定义单元格将是更好的解决方案。告诉我你是否问我回答的问题,如果不是,那么我会检查我的回答谢谢。

    【讨论】:

    • 我不想更改 UITableView 的宽度,因为这会影响第二部分中的其他行。我想使用自定义单元格方法,但想知道实现此功能的正确方法。
    • @MattPrice 所以你想知道如何实现自定义单元格。
    • @vishiphone 看看下面我的回答。我已经做了我想做的事。我无法将其标记为已回答 10 小时
    【解决方案2】:

    我设法使用以下方法制作了我想要的东西,这不是最好的方法或最干净的方法,但由于 StackOverFlow 中没有人给出更好的建议,我认为我最好回答这个问题。

    我对前 3 个 UITableViewCell 进行了子类化,并设置了一个框架大小以考虑我的图像大小。

    - (void)setFrame:(CGRect)frame {
    float cellWidth = (frame.size.width - 345);
    frame.size.width = cellWidth;
    
    [super setFrame:frame];
    

    }

    然后在我的 UITableViewController 的 viewDidLoad 中,我使用 tableview 中的第一个静态单元格作为 IBOutlet(“firstCell”)创建和定位 UIImageView。然后我设置了 autoResizingMask 来对旋转进行排序,最后将 UIImageView 添加到视图中。

    //Create and position the image view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((firstCell.frame.size.width), (firstCell.frame.origin.y + 70), 300, 137)];
    
    // Add border and round corners of image view to make style look a little like tableviewcells
    [imageView.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.5].CGColor];
    [imageView.layer setBorderWidth:2.0];
    [imageView.layer setCornerRadius:5.0];
    [imageView setClipsToBounds:YES];
    
    //Set the image in the image view
    UIImage *image = [UIImage imageNamed:@"defaultPicture.png"];
    imageView.image = image;
    
    //Set resizing of image view for when view is rotated
    imageView.contentMode = UIViewContentModeRedraw;
    imageView.autoresizingMask = UIViewContentModeScaleAspectFit;
    
    //Add tap gesture for imageview to initiate taking picture.
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(takePicture:)];
    [imageView addGestureRecognizer:imageViewTap];
    
    //Add image view to view
    [self.view addSubview:imageView];
    

    这还没有经过全面测试,我敢肯定这不是一个很好的实现,但它是我所追求的效果的起点。如果您知道更好的方法,请回复。

    注意:以上代码是为我在 iPad 上的应用编写的,但截图来自我在 iPhone 上所做的测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多