2013-10-08 32 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

相關問題