我的prepareForReuse不能正常工作。我有一个UITableView,它应该只在表的第一部分的第一行中有一个login UIButton。但是,在prepareForReuse中,当我删除login按钮时,它会停留在下一批行中。(演示-> 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;
}发布于 2014-03-24 18:56:53
如果您这样做了:
self.loginButton = nil;然后试着这样做:
[self.loginButton removeFromSuperview];它不会起作用,因为你已经删除了引用。
考虑对此单元格使用不同的单元格标识符,因为如果要添加和删除按钮,这并不是真正的重用。也可以考虑隐藏/显示按钮。如果您想要删除它,则将代码更改为:
- (void)prepareForReuse
{
[self.loginButton removeFromSuperview];
self.loginButton = nil;
self.textLabel.text = nil;
[super prepareForReuse];
}看起来你也从来没有设置过:
cell.loginButton = loginButton;因此,该单元格可能无论如何都没有引用可用…
https://stackoverflow.com/questions/22607293
复制相似问题