UITableViewCell中的最终UIButton是不可变的

时间:2017-01-12 23:18:11

标签: ios objective-c uitableview uibutton

我有一个UITableView(以及相关的Cell),其中包含可变数量的项目。每个单元格内部都是与正在显示的对象关联的值。还有一个按钮,允许用户说明正在显示的任务(来自对象属性)是否完成。它应该改变颜色并变得活跃。这是有效的,除了列表中的最后一项。以下是无效的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TasksItem"];
    [BAPAppDelegate setBorder:[cell viewWithTag:300] withBorder:[BAPAppDelegate darkGreyColor]];
    [BAPAppDelegate setBorder:[cell viewWithTag:301] withBorder:[UIColor blackColor]];
    [BAPAppDelegate setBorder:[cell viewWithTag:1000] withBorder:[UIColor blackColor]];
    ServiceRequest *sr = (mListTasks)[indexPath.row];
    [(UILabel *)[cell viewWithTag:110] setText:sr.vSRType];
    UILabel *smry = (UILabel *)[cell viewWithTag:301];
    smry.text = [NSString stringWithFormat:@"%@", [sr vMTSmry]];    // check if completed
    int cnt;
    int complete;
    UIButton* btn;
    cnt = [sr getBeforeRequired];
    complete = [sr getBeforeCompleted];
    markCompleteButton = YES;
    btn = (UIButton *)[cell viewWithTag:120];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getDuringRequired];
    complete = [sr getDuringCompleted];
    btn = (UIButton *)[cell viewWithTag:121];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getAfterRequired];
    complete = [sr getAfterCompleted];
    btn = (UIButton *)[cell viewWithTag:122];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getCheckRequired];
    complete = [sr getCheckCompleted];
    btn = (UIButton *)[cell viewWithTag:123];
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"check_red.png"] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }
    btn = (UIButton *)[cell viewWithTag:1000];
    [btn setTag:indexPath.row + 1001];
    if (markCompleteButton) {
        [btn setBackgroundColor:[BAPAppDelegate greenColor]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setEnabled:YES];
        [btn setTitle:@"YEP" forState:UIControlStateNormal];
        NSLog(@"YEP %ld", (long)indexPath.row);
    } else {
        [btn setEnabled:NO];
        NSLog(@"NOPE %ld", (long)indexPath.row);
    }
    [cell prepareForReuse];
    return cell;
}

正如预期的那样,日志显示“YEP 8”,但按钮本身没有任何变化。列表中较早的其他按钮会显示并按预期执行操作。

非常感谢任何见解!

1 个答案:

答案 0 :(得分:0)

我最终继承了UITableViewCell并将UITableViewCell从我的故事板连接到我的子类,然后将按钮的出口连接到子类。另外,我将按钮的动作设置为子类。

<强> TasksCell.h

@protocol TasksCellDelegate <NSObject>
- (void)buttonPressed:(UIButton *)sender;
@end

@interface TasksCell : UITableViewCell
@property (weak, nonatomic) id<TasksCellDelegate> delegate;
@property (weak, nonatomic) IBOutlet UIButton *button;

- (IBAction)buttonPressed:(UIButton *)sender;
@end

<强> TasksCell.m:

#import "TasksCell.h"

@implementation TasksCell
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (IBAction)buttonPressed:(UIButton *)sender {
    [self.delegate buttonPressed:sender];
}
@end

<强> TasksViewController.h

#import "TasksCell.h"

<强> TasksViewController.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TasksCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"TasksItem"
                                           forIndexPath:indexPath];
    [BAPAppDelegate setBorder:[cell viewWithTag:300] withBorder:[BAPAppDelegate darkGreyColor]];
    [BAPAppDelegate setBorder:[cell viewWithTag:301] withBorder:[UIColor blackColor]];
    [BAPAppDelegate setBorder:[cell viewWithTag:1000] withBorder:[UIColor blackColor]];
    ServiceRequest *sr = (mListTasks)[indexPath.row];
    [(UILabel *)[cell viewWithTag:110] setText:sr.vSRType];
    UILabel *smry = (UILabel *)[cell viewWithTag:301];
    smry.text = [NSString stringWithFormat:@"%@", [sr vMTSmry]];    // check if completed
    int cnt;
    int complete;
    UIButton* btn;
    cnt = [sr getBeforeRequired];
    complete = [sr getBeforeCompleted];
    markCompleteButton = YES;
    btn = (UIButton *)[cell viewWithTag:120];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getDuringRequired];
    complete = [sr getDuringCompleted];
    btn = (UIButton *)[cell viewWithTag:121];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getAfterRequired];
    complete = [sr getAfterCompleted];
    btn = (UIButton *)[cell viewWithTag:122];
    if (cnt > 0) {
        [btn setHidden:NO];
        [btn setTitle:[NSString stringWithFormat:@"%i", cnt] forState:UIControlStateNormal];
    } else {
        [btn setTitle:@"0" forState:UIControlStateNormal];
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
    }
    cnt = [sr getCheckRequired];
    complete = [sr getCheckCompleted];
    btn = (UIButton *)[cell viewWithTag:123];
    if (complete < cnt) {
        [btn setTitleColor:[BAPAppDelegate redColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"aprsi_check_red.png"] forState:UIControlStateNormal];
        [self markComplete:NO];
    } else {
        [btn setTitleColor:[BAPAppDelegate greenColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"aprsi_check.png"] forState:UIControlStateNormal];
    }
    [cell.button setTag:indexPath.row + 1001];
    if (markCompleteButton) {
        cell.button.backgroundColor = [BAPAppDelegate greenColor];
        [cell.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.button setEnabled:YES];
        NSLog(@"YEP %ld", (long)indexPath.row);
    } else {
        [cell.button setTitleColor:[BAPAppDelegate darkGreyColor] forState:UIControlStateDisabled];
        [cell.button setBackgroundColor:[BAPAppDelegate lightGreyColor]];
        [cell.button setEnabled:NO];
        NSLog(@"NOPE %ld", (long)indexPath.row);
    }
    [cell prepareForReuse];
    return cell;
}

允许按钮按预期工作,我在TasksViewController中保留了按钮委托,但同样可以轻松地将其移动到子类。希望这可以帮助别人!