2014-03-24 34 views
2

我的prepareForReuse無法正常工作。我有一個UITableView,應該只在表的第一部分的第一行中有一個loginUIButton。但是,當在prepareForReuse中,我刪除了login按鈕,它保持並進入下一批行。 (視頻說明 - >http://pixori.al/8g3v自定義UITableViewCell prepareForReuse不按預期方式工作

這裏是我的自定義UITableViewCell

#import "MAGradeCell.h" 

@implementation MAGradeCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; } 

-(void)layoutSubviews{ 
    [super layoutSubviews]; } 

- (void)prepareForReuse { 
    self.loginButton = nil; 
    [self removeFromSuperview]; 
    [self.loginButton removeFromSuperview]; 
    self.textLabel.text = nil; 

    [super prepareForReuse]; } 

/* 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }*/ 

@end 

和我的ViewController的那臺細胞組成部分(cellForRowAtIndexPath)。即在那裏我把QBFlatButton一切:

- (MAGradeCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CellIdentifier"; 
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    MAGradeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


// // Redefine layout variables in method from `viewDidLoad` 
    CGFloat inset = 20; // For padding 


    if (! cell) { 
     cell = [[MAGradeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier cellForRowAtIndexPath:indexPath]; 
    } 


    // Sets up attributes of each cell 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.detailTextLabel.textColor = [UIColor whiteColor]; 
    QBFlatButton* loginButton = nil; 

    if (indexPath.section == 0) { 
     if (indexPath.row == 0) { 
      [self configureHeaderCell:cell title:@"Grades"]; 

       UIView *cellView = cell.contentView; 
       loginButton = [[QBFlatButton alloc] initWithFrame:CGRectMake((cellView.frame.size.width - (80 + inset)), 18, 80, (cellView.frame.size.height -(cellView.frame.size.height/2)))]; 
       [loginButton addTarget:self action:@selector(loginButtonWasPressed)forControlEvents:UIControlEventTouchUpInside]; 
       loginButton.faceColor = [UIColor grayColor]; 
       loginButton.sideColor = [UIColor clearColor]; 

       loginButton.radius = 6.0; 
       loginButton.margin = 4.0; 
       loginButton.depth = 3.0; 
       loginButton.alpha = 0.3; 

       loginButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20]; 
       [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
       [loginButton setTitle:@"Login" forState:UIControlStateNormal]; 
       [cellView addSubview:loginButton]; 
     } else { 
      cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      MAGradeClient *grade = [[MAGradeClient alloc] init]; 
      [self configureGradesCell:cell grade:grade]; 
     } 
    } else if (indexPath.section == 1) { 
     if (indexPath.row == 0) { 
      [self configureHeaderCell:cell title:@"Hourly Forecast"]; 
     } 
     else { 
      // Get hourly weather and configure using method 
      MACondition *weather = [MAManager sharedManager].hourlyForecast[indexPath.row - 1]; 
      [self configureHourlyCell:cell weather:weather]; 
     } 
    } 
    else if (indexPath.section == 2) { 
     if (indexPath.row == 0) { 
      [self configureHeaderCell:cell title:@"Daily Forecast"]; 
     } 
     else if (indexPath.section == 2) { 
      // Get daily weather and configure using method 
      MACondition *weather = [MAManager sharedManager].dailyForecast[indexPath.row - 1]; 
      [self configureDailyCell:cell weather:weather]; 
     } 
    } 

    return cell; 
} 
+0

添加視頻鏈接 – AndrewSB

回答

3

如果你這樣做:

self.loginButton = nil; 

,然後嘗試做:

[self.loginButton removeFromSuperview]; 

它不會工作,因爲你已經nilled的參考。

考慮爲這個單元格使用不同的單元格標識符,因爲如果您要添加和刪除按鈕,它不是純粹的重用。考慮也只是隱藏/顯示按鈕。如果你想刪除它,然後修改代碼:

- (void)prepareForReuse 
{ 
    [self.loginButton removeFromSuperview]; 
    self.loginButton = nil; 

    self.textLabel.text = nil; 

    [super prepareForReuse]; 
} 

它也像你是不是曾經設置:

cell.loginButton = loginButton; 

所以電池可能沒有這樣用一個參考..

+0

我翻轉了這兩個,刪除之前刪除,沒有變化。 – AndrewSB

+0

我會把'cell.loginButton = loginButton'放在自定義類或視圖控制器中嗎?我不完全理解它的用途 – AndrewSB

+0

在視圖控制器中。如果視圖控制器沒有給單元格引用它添加的按鈕,那麼單元格稍後將不能使用它。所以,就在'[cellView addSubview:loginButton];'之後。 – Wain

相關問題