Objective c 在UITableViewCell中居中显示200x150图像
我需要在UITableViewCell中显示一个以200x150为中心的图像。我能够将图像添加到一个标准的单元格类型中(它将图像缩小到合适的大小——这不起作用)。然后,我尝试通过设置图像的边界和帧来重新绘制它(这导致了我的图像和其他行之间的重叠) 我有一个从UITableViewCell继承的自定义类:Objective c 在UITableViewCell中居中显示200x150图像,objective-c,uitableview,uistoryboard,Objective C,Uitableview,Uistoryboard,我需要在UITableViewCell中显示一个以200x150为中心的图像。我能够将图像添加到一个标准的单元格类型中(它将图像缩小到合适的大小——这不起作用)。然后,我尝试通过设置图像的边界和帧来重新绘制它(这导致了我的图像和其他行之间的重叠) 我有一个从UITableViewCell继承的自定义类: #import "WCPictureViewCell.h" @implementation WCPictureViewCell - (id)initWithStyle
#import "WCPictureViewCell.h"
@implementation WCPictureViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.frame = CGRectMake(5,5,210,160);
self.bounds = CGRectMake(5,5,210,160);
self.imageView.frame = CGRectMake(0,0,200,150);
self.imageView.bounds = CGRectMake(0,0,200,150);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
此代码与我的表格的其余部分产生图片重叠:
这是我的控制器中的tableView:cellForRowAtIndexPath:method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(cellPropertyMap>0) {
static NSString *CellIdentifier = @"DetailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[[cell textLabel] setText: (NSString *)[[self displayLabels]objectAtIndex:cellPropertyMap]];
[[cell detailTextLabel] setText: (NSString *)[[self displayData]objectAtIndex:cellPropertyMap++]];
} else {
static NSString *CellIdentifier = @"PictureCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//if(cell == nil) {
//}
// Configure the cell...
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [cat friendlyURLPath]]];
cell.imageView.image = [UIImage imageWithData: imageData];
cellPropertyMap++;
return cell;
}
return cell;
}
如何强制表格单元格相互尊重大小而不重叠?如何使图像居中?您需要使用
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath)
{
// add logic to determine if this indexPath is an image...
if (indexPath.row == 0) // first row in a section is an image?
return (150.0f); // this row is an image
else
return (44.0f); // this is a standard data row
}
返回TableView行的正确高度
第二步是使图像居中,有各种技术。试一试
UIImageView *yourImage = ...;
// set frame and auto-resizing mask
yourImage.frame = CGRectMake(tableView.bounds.size.width / 2) - (yourImage.size.width / 2), 0, yourImage.size.width, yourImage.size.height);
yourImage.autoResizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// add UIImage to the cell contentView
[cell.contentView addSubview:yourImage];
不要设置cell.imageView属性,该属性在单元格中定义左对齐的图像
最后,从你的问题出发,你应该考虑懒惰加载图像,使用iNIT.CONTURNET URL在UI线程上不会产生良好的用户体验。
你需要使用
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath)
{
// add logic to determine if this indexPath is an image...
if (indexPath.row == 0) // first row in a section is an image?
return (150.0f); // this row is an image
else
return (44.0f); // this is a standard data row
}
返回TableView行的正确高度
第二步是使图像居中,有各种技术。试一试
UIImageView *yourImage = ...;
// set frame and auto-resizing mask
yourImage.frame = CGRectMake(tableView.bounds.size.width / 2) - (yourImage.size.width / 2), 0, yourImage.size.width, yourImage.size.height);
yourImage.autoResizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// add UIImage to the cell contentView
[cell.contentView addSubview:yourImage];
不要设置cell.imageView属性,该属性在单元格中定义左对齐的图像
最后,从你的问题出发,你应该考虑懒惰的加载图像,使用UI上的ItItNo.CONTURNET URL将不会产生良好的用户体验。
为高度问题而工作,我将如何对图像进行中心?你的IIGIG.FIGH给了我一个错误。上面说UIImage没有边框属性对不起,这应该是UIImageView*YourImages解决了高度问题,我该如何将图像居中?YourImages.frame给了我一个错误。它说UIImage没有框架属性对不起,这应该是UIImageView*yourImage