更改UITableViewCell宽度以适合单元格外的UIImageView

时间:2012-05-02 18:58:41

标签: iphone objective-c ios uitableview

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

Screenshot of what i'm trying to achieve

我通过使用以下代码为第1部分中的单元格继承UITableViewCell来完成以下操作

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

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我设法使用以下内容生成我想要的内容,这绝对不是最好的方法或最干净的方式,但是由于StackOverFlow中没有人给出任何更好的建议,我认为我最好回答这个问题。

我将前3个UITableViewCells子类化并设置帧大小以考虑我的图像大小。

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

[super setFrame:frame];

}

然后在我的UITableViewController的viewDidLoad中,我使用tableview中的第一个静态单元格创建并定位UIImageView作为IBOutlet(“firstCell”)。然后我设置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];

landscape portrait

这尚未经过全面测试,我确信这不是一个很好的实现,但它是我所追求的效果的起点。如果您知道更好的方法,请回复。

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

相关问题