设置在tableview中添加到单元格的图像的大小 [英] setting the size of the image added to cell in tableview
问题描述
我正在从plist中将图像添加到我的单元格视图中。此外,我对使用网址保存到plist的图像也有同样的问题。它们的大小不同。我想将它们调整到一定的大小,以便它们看起来都一样。我试图使用:
I am adding images to my cell view from a plist. Also I have the same issue with images that I save to the plist using a url as well. They are different sizes. I'd like to resize them to certain size so they would all appear the same. I have tried to use:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
它不起作用。我尝试过其他一些东西并没有成功。我查了一堆东西,找不到任何有用的东西。这是我的行方法单元格:
It did not work. I have tried a bunch of other things and did not succeed. I have looked up a bunch of stuff and can't find anything helpful. Here is my cell for row method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
} else {
cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
// Construct the expected URL for the image.
NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
// Attempt to load the image at the above URL.
cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
if (!cell.imageView.image)
cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
}
return cell;
}
对于我的生活,我无法理解这一点。我知道这可能很容易解决,但我想不出别的。
For the life of me I can't figure this out. I know it might be a easy fix but i can't think of anything else.
编辑:
根据建议,我创建了一个自定义单元格,在那里声明了layoutSubviews,然后按如下方式对单元格进行了子类化:
As suggested, I created a custom cell, declared layoutSubviews in there and then subclassed the cell as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyCostumCell *cell = (MyCostumCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCostumCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
//cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:1];
} else {
cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
// Construct the expected URL for the image.
NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
// Attempt to load the image at the above URL.
cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
if (!cell.imageView.image)
cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
}
return cell;
}
同样,它不起作用。这是屏幕截图:
Again, it did not work. Here is the screen shot:
推荐答案
您将继承UITableViewCell。但你不必创建Dante提出的全新视图层次,但覆盖 -layoutSubviews
you would subclass UITableViewCell. but you dont have to create a whole new view hierachy as proposed by Dante, but overwrite -layoutSubviews
@interface VideoItemCell : UITableViewCell
@end
@implementation VideoItemCell
-(void)layoutSubviews
{
[super layoutSubviews];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100);
}
@end
在tableview控制器中
In the tableview controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
VideoItemCell *cell = (VideoItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[VideoItemCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
//…
return cell;
}
这篇关于设置在tableview中添加到单元格的图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!