自定义 UITableViewCell prepareForReuse 未按预期工作 [英] Custom UITableViewCell prepareForReuse not working as expected

查看:32
本文介绍了自定义 UITableViewCell prepareForReuse 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 prepareForReuse 工作不正常.我有一个 UITableView 应该有一个 login UIButton 在表格的第一部分的第一行.但是,当我在 prepareForReuse 中删除 login 按钮时,它会停留并进入下一批行.(视频说明 -> http://pixori.al/8g3v )

My prepareForReuse isn't working properly. I have a UITableView that is supposed to have a login UIButton in the first row of the first section of the table only. But when, in prepareForReuse, I remove the login button, it stays and comes onto the next batch of rows. (video to illustrate -> 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 和所有东西放在哪里:

and the part of my viewcontroller that sets the cells up (cellForRowAtIndexPath). ie where I put the QBFlatButton and everything:

- (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];

它不起作用,因为您已经取消了引用.

it won't work because you already nilled the reference.

考虑为此单元格使用不同的单元格标识符,因为如果您要添加和删除按钮,它实际上并不是纯粹的重用.还考虑只隐藏/显示按钮.如果要删除它,请将代码更改为:

Consider using a different cell identifier for this cell as it isn't really a pure reuse if you are adding and removing a button. Consider also just hiding / showing the button. If you want to remove it then change the code to:

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

    self.textLabel.text = nil;

    [super prepareForReuse];
}

看起来您也从未设置过:

It also looks like you aren't ever setting:

cell.loginButton = loginButton;

因此该单元格可能没有任何引用可供使用...

so the cell may not have a reference to use anyway...

这篇关于自定义 UITableViewCell prepareForReuse 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆