2013-03-16 60 views
2

我想在我的UITableView只在某些單元格中顯示圖像。這裏有一個我configureCell方法:有條件地顯示圖像在UITableViewCell

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    UIImage *ribbon = [UIImage imageNamed:@"ribbon.png"]; 
    UIImageView *ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)]; 
    [ribbonView setImage:ribbon]; 
    [cell addSubview:ribbonView]; 
    if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) { 
     cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1]; 
     ribbonView.hidden = NO; 
    } 
    else { 
     cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
     ribbonView.hidden = YES; 
    } 
} 

而這裏的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // set up the cell... 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

這並不完全工作,因爲首先,所有的細胞吸引他們ribbonView,無論info.visited值。我已經通過了if/else,並且看到它被隱藏的代碼被擊中,但是。但是,如果我從列表中導航,然後返回,則可以看到正確的色帶狀態。滾動表格視圖再次打破它。

雖然字體的顏色總是正確的。

回答

5

如果您正在重用的細胞,那麼它有可能成爲你添加多個ribbonView子視圖的細胞,所以即使info.visited當前indexPathNO,存在於細胞的另一個ribbonView吃剩的,您仍然可以看到。

做的是讓事情肯定你永遠只能有一個ribbonView子視圖,它可以通過繼承UITableViewCell並添加ribbonView性質的細胞,它被設置做到無論在你的配置方法刪除舊ribbonViews,或更好一次並添加到單元的視圖層次中,然後您可以在配置方法中訪問並設置hiddenNOYES

編輯:單元格文字顏色將始終正確,因爲您正在更改單元格的視圖層次結構中的一個實例UILabel的顏色。我希望你會看到相同的錯誤行爲,而不是你的配置方法在每次配置時在單元格中添加一個新的子視圖UILabel

編輯:代碼嘗試

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    static NSInteger ribbonTag = 12345; 
    StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    // re-use a ribbonView if one's already been added to this cell 
    UIImageView *ribbonView = [cell.contentView viewWithTag:ribbonTag]; 
    if (!ribbonView){ 
     ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)]; 
     ribbonView.tag = ribbonTag; 
     UIImage *ribbon = [UIImage imageNamed:@"ribbon.png"]; 
     [ribbonView setImage:ribbon]; 
     // add subviews to contentView 
     [cell.contentView addSubview:ribbonView]; 
    } 
    if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) { 
     cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1]; 
     ribbonView.hidden = NO; 
    } 
    else { 
     cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
     ribbonView.hidden = YES; 
    } 
} 
+0

謝謝!這個解決方案和@ pedros的解決方案都可以工作 – hodgesmr 2013-03-16 17:28:59

+0

我錯過了'if(!ribbonView)',這個難題的關鍵部分,我現在也在工作,所以謝謝你的未來! :) – Luke 2013-04-12 11:12:36

0

那絕對是在執行的

tableView:cellForRowAtIndexPath: 

一個問題,你需要始終調用

dequeueReusableCellWithIdentifier:forIndexPath: 

,然後打電話給你的配置方法

configureCell:atIndexPath:(NSIndexPath *)indexPath 

編輯:

另外,我想你也需要做的,而不是[cell.contentView addSubView:...] [細胞addSubView:...]

+0

謝謝,我編輯顯示我的cellForRowAtIndexPath。我相信我已經正確地做到了這一點? – hodgesmr 2013-03-16 17:13:35

+0

不幸的是,這仍然沒有做到這一點。細胞開始正常,但一旦他們從屏幕上滾動然後回來,他們就有絲帶,他們不應該。也許我需要按照Erik H的建議和子類來做。 – hodgesmr 2013-03-16 17:20:57

+1

@hodgesmr問題在於,每次配置單元格時都要調用addSubview,因此當單元格被重用時,它會添加多個子視圖,其中一些隱藏,一些則不會。 – ehope 2013-03-16 17:21:53

1

如果您在tableView:cellForRowAtIndexPath:方法使用dequeueReusableCellWithIdentifier:forIndexPath:。每次重新使用單元格時,都會創建一個新的UIImageView並將其放在最後一個單元格上。

但是要解決這個問題,你不需要繼承子類。還沒有,因爲你的細胞仍然很簡單。如果你想添加更多的子視圖,那麼子類是唯一的選擇。

一個解決方案,我能想到的會是這樣:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    UIImageView *ribbonView = nil; 

    //My code: 
    for (UIView *childView in cell.subviews) { 
     if([childView isKindOfClass:[UIImageView class]] { 
      ribbonView = childView; 
      break; 
     } 
    } 
    //Note: this doesnt work if you have more than one UIImageView in your cell. 
    if(ribbonView == nil) { 
     UIImage *ribbon = [UIImage imageNamed:@"ribbon.png"]; 
     ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)]; 
     [ribbonView setImage:ribbon]; 
     [cell addSubview:ribbonView]; 
    } 
    //Ends here. 

    if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) { 
     cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1]; 
     ribbonView.hidden = NO; 
    } 
    else { 
     cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
     ribbonView.hidden = YES; 
    } 
} 

試試吧,告訴我,如果它的工作原理。

祝你好運。

+0

這和接受的答案都適用。 – hodgesmr 2013-03-16 17:43:01