我的 prepareForReuse
工作不正常。我有一个 UITableView
应该只有在表的第一部分的第一行中有一个 login
UIButton
。但是,当我在 prepareForReuse
中删除 login
按钮时,它会停留并进入下一批行。 (视频说明 -> http://pixori.al/8g3v)
这是我的自定义 UITableViewCell
:
#import "MAGradeCell.h"
@implementation MAGradeCell
- (id)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)reuseIdentifier cellForRowAtIndexPathNSIndexPath *)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)setSelectedBOOL)selected animatedBOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }*/
@end
以及设置单元格的 View Controller 部分 (cellForRowAtIndexPath
)。即我把 QBFlatButton
和所有东西放在哪里:
- (MAGradeCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 actionselector(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;
所以单元格可能没有使用的引用...
关于ios - 自定义 UITableViewCell prepareForReuse 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607293/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |