2012-07-24 41 views
5

編輯:UIViewContentModeScale模式觸摸(高亮)

我得到創建一個定製電池解決了該問題(然而,還有另外questions)但是,爲什麼默認的單元格不工作仍然是一個謎...

科裏的答案沒有解決這個問題,但clipsToBounds真的是一個點,我讚賞他的持續幫助。所以我給他賞金,但不接受他的答案作爲答案。在風中,答案還是....

================================

我將單元格圖像的UIViewContentModeScale模式設置爲AspectFill(左圖),並且圖像正確顯示(填充圖像視圖但保留方面)。但是,在觸摸並按住單元格之後,圖像會發生變化:imageView的框架和contentViewMode會更改。 (請參閱下面左圖中的第一個單元格)

這是真的有線,我可以相信,如基本的東西可能會出錯,所以一定有一些東西我錯過了。

環境:Xcode 4.3 iOS5.1模擬器&設備。 enter image description here

更新:

我設置使用故事板的檢查員的UIViewContentMode。

順便說一句,我用SDWebImage/UIImageView + WebCache.h。

UPDATE2:

我明確設置內容模式的代碼,但問題仍然存在。 [cell.imageView setContentMode:UIViewContentModeScaleAspectFill];

UPDATE4:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AlbumCell"]; 

    } 
    //[cell.imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    //[cell.imageView setClipsToBounds:YES]; 
    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    Album *album = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = [album.title description];  
    cell.detailTextLabel.text = [self.userVisiableDateFormatter stringFromDate:album.releaseDate]; 


    //[cell.imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    [cell.imageView setImageWithURL:[NSURL URLWithString:album.coverThumbnailUrl] 
       placeholderImage:[self placeHolderImage]]; 

} 

enter image description here

+0

用什麼方法設置內容模式?告訴我們該方法的來源。 – 2012-07-25 04:06:55

+0

@rob我使用故事板中的歸屬檢查器設置歸因。 – pierrotlefou 2012-07-26 08:47:27

+0

嘗試使用scaleToFill內容模式 – Mrunal 2012-07-26 09:41:25

回答

8

我能找到幾個不同的東西,可能是問題所在。

首先,它嘗試設置clipsToBounds屬性:

img1.contentMode = UIViewContentModeScaleAspectFill; 
img1.clipsToBounds = YES; 

二是:

看不到這些模式之間的區別是,自動的UITableViewCell大小的圖像視圖,以適應其原因圖像的寬高比。內容模式僅在視圖的寬高比與圖像不同時才起作用。

要麼覆蓋自定義UITableViewCell類中的layoutSubviews,要麼自己創建一個圖像視圖(不要使用表視圖單元提供的視圖)。

編輯1:

看你的代碼後,問題可能在於你如何處理你的配置。指針可能在不應該的時候被釋放。試試這2個選項:

選項1: 保持您的當前傳遞配置方法爲新方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AlbumCell"]; 

    } 

    return [self configureCell:cell atIndexPath:indexPath]; 
} 

- (UITableViewCell*)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* returnCell = cell; 

    Album *album = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    returnCell.textLabel.text = [album.title description];  
    returnCell.detailTextLabel.text = [self.userVisiableDateFormatter stringFromDate:album.releaseDate]; 


    [returnCell.imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    [returnCell.imageView setClipsToBounds:YES]; 

    [returnCell.imageView setImageWithURL:[NSURL URLWithString:album.coverThumbnailUrl] 
       placeholderImage:[self placeHolderImage]]; 

    return returnCell; 
} 

選項2 :(我的推薦) 在委託方法中配置單元格。這裏可以這樣做,這不是一個壞習慣。這是委託方法在那裏配置單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AlbumCell"]; 

    } 


    Album *album = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = [album.title description];  
    cell.detailTextLabel.text = [self.userVisiableDateFormatter stringFromDate:album.releaseDate]; 

    [cell.imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    [cell.imageView setClipsToBounds:YES]; 

    [cell.imageView setImageWithURL:[NSURL URLWithString:album.coverThumbnailUrl] 
       placeholderImage:[self placeHolderImage]]; 


    return cell; 
} 
+0

+1。但是,我將clipstoBounds設置爲true,但問題仍然存在。 – pierrotlefou 2012-07-27 05:16:09

+0

您使用自定義單元格還是默認單元格?我試圖自己重新創建錯誤以幫助診斷問題。 – random 2012-07-27 20:52:10

+0

我正在使用默認單元格。 – pierrotlefou 2012-07-28 05:48:29

3

也爲高亮狀態設置相同的圖像。

3

可能會出現這種情況,您需要設置自動調整大小(或者如果您已經瞭解該術語,請使用struts彈簧)。

在一個應用程序中,我有左邊與你相似的圖像的自定義單元格

,我有我的自動尺寸設置爲如下圖:

enter image description here

注意,我這設置爲使單元框架內的尺寸保持不變,並且指定頂部,底部和左側的尺寸以在兩側具有相當大的間隙來擁抱這些邊緣。

除此之外,我相信你是正確的方面填充是該設置的選擇。 clipToBounds確實不重要;只有當這個視圖是另一個視圖的子視圖時纔有必要,在這個視圖中,它應該出現在它的尺寸比它的子視圖大的視圖中。

+0

約翰,我試過但沒有運氣。 – pierrotlefou 2012-08-01 10:05:51

+0

還請注意,我爲此使用自定義單元格。這是一段時間,但我似乎記得有類似的問題w.r.t.能夠根據需要使用默認單元格在頂部,底部和左側建立「空白」。如果你有興趣有空白和特定的方形圖像視圖,你可能需要做同樣的事情來保證你想要什麼。如果您對用圖像填充空間感到滿意,但希望初始圖像也填充空間,則可能必須在單元格圖像字段中使用較大的默認圖像。無論如何,我的自定義單元非常容易創建。 – 2012-08-01 16:42:02

2

我遇到類似的問題,具有子類UIImageView屬性的子類UITableViewCell。將此屬性的名稱從'imageView'更改爲其他內容可以解決我的問題。

+1

謝謝!爲我解決了這個問題! – schirrmacher 2013-09-17 16:58:27