2013-05-09 149 views
0

我在UITableView中有一個導航菜單。如何正確重新加載細胞

我的細胞加載以下:

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

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)]; 
     topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; 
     [cell.contentView addSubview:topSplitterBar]; 
    } 

    // Set a boolean to determine if the item in the menu is the currently displayed VC on the main stack 
    BOOL currentDisplayed = NO; 


    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1]; 
    cell.textLabel.font = [UIFont systemFontOfSize:18.0f]; 
    cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1]; 
    cell.textLabel.shadowOffset = CGSizeMake(0, 1); 

    UIView *selectedBg = [[UIView alloc] init]; 
    selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; 
    selectedBg.clipsToBounds = YES; 
    UIView *topSplitterBar2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)]; 
    topSplitterBar2.backgroundColor = [UIColor yellowColor]; 
    [selectedBg addSubview:topSplitterBar2]; 
    [cell setSelectedBackgroundView:selectedBg]; 

    // Configure the cell... 
    if (indexPath.section == 0) { 
     if ([self.slidingViewController.topViewController isKindOfClass:[MESProfileViewController class]]) { 
      currentDisplayed = YES; 
     } 
     cell.textLabel.text = NSLocalizedString(@"LMDisplayName", @"Left Menu - displayname"); 
    } else { 
     switch (indexPath.row) { 
      case 0: { 
       if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) { 
        currentDisplayed = YES; 
       } 
       cell.textLabel.text = NSLocalizedString(@"LMGames", @"Left Menu - Games"); 
       break ; 
      } 
      case 1: { 
       cell.textLabel.text = NSLocalizedString(@"LMShare", @"Left Menu - Share"); 
       break ; 
      } 
      case 2: { 
       cell.textLabel.text = NSLocalizedString(@"LMRate", @"Left Menu - Rate"); 
       break ; 
      } 
      case 3: { 
       if ([self.slidingViewController.topViewController isKindOfClass:[MESSettingsViewController class]]) { 
        currentDisplayed = YES; 
       } 
       cell.textLabel.text = NSLocalizedString(@"LMSettings", @"Left Menu - Settings"); 
       break ; 
      } 
      case 4: { 
       cell.textLabel.text = NSLocalizedString(@"LMHelp", @"Left Menu - Help"); 
       break ; 
      } 
      case 5: { 
       cell.textLabel.text = NSLocalizedString(@"LMLogout", @"Left Menu - Log out"); 
       break ; 
      } 
     } 
    } 

    if (currentDisplayed) { 
     // This is for the current item that is being displayed 
     cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; 
     UIImage *arrowImage = [UIImage imageNamed:@"selectedArrow"]; 
     UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(240, 10, arrowImage.size.width, arrowImage.size.height)]; 
     arrow.image = arrowImage; 
     [cell.contentView addSubview:arrow]; 
    } else { 
     cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0]; 
     NSLog(@"%@",cell.textLabel.text); 
     NSLog(@"%@",cell.contentView.subviews); 
    } 

    return cell; 
} 

然後didSelectCell內我重裝的一組單元(其可被示出爲當前小區)改變背景。

NSIndexPath* displayNameRow = [NSIndexPath indexPathForRow:0 inSection:0]; 
NSIndexPath* gamesRow = [NSIndexPath indexPathForRow:0 inSection:1]; 
NSIndexPath* settingsRow = [NSIndexPath indexPathForRow:3 inSection:1]; 
NSArray* rowsToReload = [NSArray arrayWithObjects:displayNameRow, gamesRow, settingsRow, nil]; 
[weakSelf.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; 

我所發現的是,再次被創建的UIView * topSplitterBar每個細胞被重新加載時間加入到cell.contentView?

我認爲重新加載單元格會清除所有子視圖並從頭開始創建。我應該如何改變這個實現來不繼續添加這些視圖。如果它是當前單元格,我還有另一個UIImageView用於添加箭頭,具有與上述相同的效果,因此最終我具有許多不需要的額外視圖的單元格。

背景: 這是一個側視圖控制器(滑入)菜單表視圖。所選單元格顯示此當前選擇已顯示爲主頂視圖控制器。

回答

0

您應該更改您的cellForRowAtIndexPath的實施,以便它不會一次又一次地添加子視圖。我寫了一個帶有評論的實施草案。

//Here you might get a reusable cell with dequeuing so your subviews are already there, just do customization on the subviews 
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    //If you haven't got a reusable cell create a new one with all default subview properties 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)]; 
    topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; 
    [cell.contentView addSubview:topSplitterBar]; 
} 

//Do all your customization afterwards, identifying cells based on indexPath 
... 

這是關於這個問題https://stackoverflow.com/a/3490460/837244接受的答案簡潔的答案(不接受)中提到溶液B將工作,但實際上它會被反覆添加子視圖,如在評論中提到。

+0

謝謝,但這似乎實際上停止添加視圖。 – StuartM 2013-05-09 12:10:59

+0

爲什麼?你添加它在語句'[cell.contentView addSubview:topSplitterBar];'如果塊 – guenis 2013-05-09 12:12:01

+0

我已經用方法的完整代碼更新了問題,以幫助解釋這裏正在做什麼 – StuartM 2013-05-09 12:12:54