自定义UITableViewCell prepareForReuse不按预期方式工作
我的prepareForReuse
工作不正常。 我有一个UITableView
应该有一个login
UIButton
在表的第一部分的第一行只。 但是,在prepareForReuse
,当我删除login
button时,它保持并进入下一批的行。 (video来说明 – > http://pixori.al/8g3v )
这是我的自定义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; }
如果你这样做:
self.loginButton = nil;
然后尝试做:
[self.loginButton removeFromSuperview];
它不会工作,因为你已经没有参考。
考虑为这个单元格使用不同的单元格标识符,因为如果你正在添加和删除一个button,它不是一个纯粹的重用。 考虑也只是隐藏/显示button。 如果你想删除它,然后将代码更改为:
- (void)prepareForReuse { [self.loginButton removeFromSuperview]; self.loginButton = nil; self.textLabel.text = nil; [super prepareForReuse]; }
它也看起来像你从来没有设置:
cell.loginButton = loginButton;
所以单元格可能没有任何使用的参考…
- 在uitableviewcell中asynchronous加载图像
- 如何获取uitableview ios中viewwithtag的UIButton的索引
- 在自定义UITableViewCell中调整UITextView的大小
- 通过单击自定义UIButton删除UITableView行
- 当UITableView完全重新加载
- 如何在两个ViewController之间传递数据,其中第一个VC有两个tableViews,第二个tableViewselect的数据应该传递给第二个VC?
- 从谷歌访问结果放置API自动完成
- 对于viewForHeaderInSection中的自定义单元格,重新加载特定的viewForHeaderInSection IOS
- 如何检索所有可见的表节标题视图