我有一个tableview,每个单元格都有图像,标题和副标题。每个单元格都有不同的图像,我无法弄清楚如何使它们的右侧相互对齐。换句话说,有些比其他更广泛,并且更进一步向着单元格的右侧,也推动了标题和副标题。我不在乎左右两边是黑条,还是顶部和底部,我只是希望它们都占用相同的空间。 我试过这是我的viewforrowatindexpath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.textLabel.text = [[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] title];
cell.detailTextLabel.text = (NSString *)[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] location];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] imageURL]]];
UIImage *theImage = [[UIImage alloc] initWithData:imageData];
cell.imageView.frame = CGRectMake(0, 0, 20, 20);
cell.imageView.image = theImage;
cell.imageView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:1.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// return it
return cell;
[imageData release];
[theImage release];
}
我认为通过设置单元格imageview的框架,图像会适合它,但是当我运行应用程序时添加该行不会改变?有没有办法在viewforrowatindexpath方法中完成这个?
答案 0 :(得分:1)
我知道您无法控制图像尺寸。
我没试过,但你可以尝试一下:
cell.imageView.clipsToBounds;
如果这个简单的步骤不起作用,无论如何 您可以尝试在将图像大小添加到cell.imageview之前设置图像大小。
<强>第一强> 将此功能添加到您的文件
- (UIImage *)resetImage:(UIImage*)originalImage {
CGSize newSize = CGSizeMake(20, 20)
CGRect imageRect = CGRectMake(0,0, newSize.width,newSize.height);
UIGraphicsBeginImageContext(newSize);
[originalImage drawInRect:SymbolRectangle];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
秒设置单元格图像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.textLabel.text = [[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] title];
cell.detailTextLabel.text = (NSString *)[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] location];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] imageURL]]];
UIImage *theImage = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = [self resetImage:theImage];
cell.imageView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:1.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// return it
return cell;
[imageData release];
[theImage release];
}
现在我不得不承认我不在xcode附近检查它,但我相信这是解决方案的方向。
祝你好运答案 1 :(得分:0)
O.K - 我认为这就是你要找的东西。
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@"could not scale image");
return newImage ;
}