[英]setting the size of the image added to cell in tableview
I am adding images to my cell view from a plist. 我正在从plist将图像添加到单元格视图中。 Also I have the same issue with images that I save to the plist using a url as well.
另外,我也有使用URL保存到plist的图像的相同问题。 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.
我知道这可能很容易解决,但我别无选择。
Edit: 编辑:
As suggested, I created a custom cell, declared layoutSubviews in there and then subclassed the cell as follows: 如建议的那样,我创建了一个自定义单元,在其中声明了layoutSubviews,然后按如下所示对单元进行了子类化:
- (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:
这是屏幕截图:
you would subclass UITableViewCell. 您将继承UITableViewCell。 but you dont have to create a whole new view hierachy as proposed by Dante, but overwrite
-layoutSubviews
但您不必按照Dante的建议创建一个全新的视图
-layoutSubviews
,而是覆盖-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
In the tableview controller 在tableview控制器中
- (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;
}
Default TableViewCell resizes imageview based on image size. 默认的TableViewCell根据图像大小调整imageview的大小。 To solve this you can use a custom cell having a custom Image View ..
为了解决这个问题,您可以使用具有自定义Image View的自定义单元格。
More info on how to create custom cell 有关如何创建自定义单元的更多信息
How do you load custom UITableViewCells from Xib files? 如何从Xib文件加载自定义UITableViewCells?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.