更改UITableViewCell宽度以适合单元格外部的UIImageView [英] Changing UITableViewCell width to fit a UIImageView outside of the cell

查看:43
本文介绍了更改UITableViewCell宽度以适合单元格外部的UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道通过以下布局实现的最佳方法/正确方法吗?我想将UIImageView放在具有静态单元格的UITableView中的UITableViewCell之外.

I would like to know the best way/correct way to achieve from following layout? I want to place an UIImageView outside of UITableViewCell in a UITableView with static cells.


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

I have done the following by subclassing UITableViewCell for the cells in section 1 using the following code

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

在UITableViewController的viewDidLoad中,添加UIImageView并根据自定义UITableViewCell的宽度对其进行定位.

In the UITableViewController's viewDidLoad I add the UIImageView and position it according to the width of the custom UITableViewCell.

这种类型的作品,但是我不确定如何处理轮换,以及我到目前为止是否做过正确的方法?

This sort of works, but i'm not sure how to deal with rotation and also if what i've done so far would be the correct way?

推荐答案

我设法使用以下方法生成了我想要的东西,这不是最好的方法,也不是最干净的方法,但是由于StackOverFlow中没有人提供任何更好的建议,我认为我最好回答这个.

I managed to produce what I wanted using the follow, this is proberly not the best way or cleanest way but as no one from StackOverFlow gave any better suggestions I thought I better answer this.

我将前三个UITableViewCells子类化,并设置框架大小以考虑图像的大小.

I subclassed the first 3 UITableViewCells and set a frame size to take into account the size of my image.

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

[super setFrame:frame];

}

然后在我的UITableViewController的viewDidLoad中,使用表视图中的第一个静态单元格作为IBOutlet("firstCell")创建并定位UIImageView.然后,我设置了autoResizingMask,它可以解决旋转问题,最后将UIImageView添加到视图中.

Then in my UITableViewController's viewDidLoad I create and position the UIImageView using the first static cell in the tableview as an IBOutlet ("firstCell"). I then set the autoResizingMask which sorts out rotation and finally add the UIImageView to the view.

//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];

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

This has not been fully tested and i'm sure isn't a great implementation, but its a starting point for the effect i'm after. Please reply if you know of a better way.

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

Note: The above code is written for my app on the iPad but screenshots are from testing I did on iPhone

这篇关于更改UITableViewCell宽度以适合单元格外部的UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆