My prepareForReuse
无法正常工作。我有一个UITableView
那应该有一个login
UIButton
仅在表的第一部分的第一行中。但当,在prepareForReuse
,我删除了login
按钮,它会保留并进入下一批行。 (视频说明->http://pixori.al/8g3v 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
以及我的视图控制器设置单元格的部分(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];
它不会工作,因为你已经清空了引用。
请考虑为此单元格使用不同的单元格标识符,因为如果您要添加和删除按钮,它实际上并不是纯粹的重用。还可以考虑隐藏/显示按钮。如果你想删除它,请将代码更改为:
- (void)prepareForReuse
{
[self.loginButton removeFromSuperview];
self.loginButton = nil;
self.textLabel.text = nil;
[super prepareForReuse];
}
它看起来也像你从来没有设置过:
cell.loginButton = loginButton;
所以单元格可能没有可以使用的引用......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)