2013-10-08 34 views
1

我正在从plist添加图像到我的单元格视图。此外,我也有与使用网址保存到plist中的图像相同的问题。他们是不同的大小。我想调整它们到一定的大小,以便它们看起来都一样。我试图使用:在tableview中设置添加到单元格的图像的大小

cell.imageView.contentMode = UIViewContentModeScaleAspectFill; 
cell.imageView.clipsToBounds = YES; 

它没有工作。我尝试了一堆其他的东西,但没有成功。我查了一堆东西,找不到有用的东西。这里是我的单元格行方法:

- (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; 
} 

对于我的生活,我无法弄清楚这一点。我知道这可能是一个简单的解决方法,但我想不出别的。

编辑:

至于建议,我创建了一个自定义单元格,宣布在那里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; 
} 

再次,它没有工作。下面是截图:

enter image description here

+0

你绝对应该做纪录片的一些阅读。你的措词是完全混淆的。在'cellForRowAtIndexPath:'你不要继承这个单元格。你使用一个分类的单元格。 – vikingosegundo

回答

7

你继承的UITableViewCell

更多信息。但你没有创建一个全新的视图层次结构所建议的但丁,但覆盖-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控制器

- (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; 
} 
+0

我试图添加方法,我得到的错误没有可见的@interface为uitableview声明选择器layoutsubview。 –

+0

你需要继承这个单元格,而不是表格视图 – vikingosegundo

+0

我该怎么做,给定我上面的代码? –

1

默认TableViewCell基于图像尺寸调整大小ImageView的。为了解决这个问题,你可以使用具有自定义图像查看自定义单元格..关于如何创建自定义单元格

How do you load custom UITableViewCells from Xib files?

+0

感谢您的链接,但它太旧了,不起作用。我也尝试过通过图像视图创建图像并为其指定标签的自定义单元格。那也没用。你可以建议什么? –

+0

答案仍然有效..你可能会尝试谷歌正确的教程一步一步,这可以帮助你做到这一点 – Shubhank

相关问题