简体   繁体   中英

UITableView Scrolling removes the UIButton value

I have an UITableview and i added UIButton as a custom view. When i press the button it changes the title for selected button but when I scroll it, it goes back to normal state. See code below...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    static NSString *Cellidentifier = @"DataTableCellId";
    STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CellView" owner:self options:nil];
    cell = nib[0];

        cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        cellButton.tag=indexPath.row;
        [cellButton addTarget:self
                   action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [cellButton setTitle:@"Save" forState:UIControlStateNormal];
        cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
        cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cellButton.tintColor = [UIColor lightGrayColor];
        cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
        cellButton.layer.borderWidth = 1.0f;
        cellButton.layer.cornerRadius = 5;
        cellButton.layer.masksToBounds = YES;
        [cell.contentView addSubview:cellButton];

    }

    return cell;
}

-(void)buttonClicked:(UIButton*)sender
{      
        [sender setBackgroundColor:[UIColor lightGrayColor]];
        [sender setTintColor: [UIColor whiteColor]];
        [sender setTitle:@"Saved" forState:UIControlStateNormal];
        [sender addTarget:self
                   action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
        sender.enabled = NO;
}

First, check your clause for not nil, it's makes no sense check your cell for nil and do not instantiate it. and because of reusable behaviour your button drops into initial state. So, check your button's enabled property

   {
    NSMutableDictionary *buttonsState;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    buttonsState = [NSMutableDictionary new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    static NSString *Cellidentifier = @"DataTableCellId";
    STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
    UIButton *cellButton = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
    if (cell == nil) {

        cell = [[STCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier]
    }
    if (cellButton == nil)
    {
        cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        cellButton.tag=indexPath.row;
        [cell.contentView addSubview:cellButton];

    }

    if ([buttonsState objectForKey:@(indexPath.row)] == nil)
    {
        [cellButton setTitle:@"Save" forState:UIControlStateNormal];
        cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
        cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cellButton.tintColor = [UIColor lightGrayColor];
        cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
        cellButton.layer.borderWidth = 1.0f;
        cellButton.layer.cornerRadius = 5;
        cellButton.layer.masksToBounds = YES;
        [cellButton addTarget:self
                       action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    }
    else
    {
        [cellButton setBackgroundColor:[UIColor lightGrayColor]];
        [cellButton setTintColor: [UIColor whiteColor]];
        [cellButton setTitle:@"Saved" forState:UIControlStateNormal];
        [cellButton addTarget:self
                       action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

-(void)buttonClicked:(UIButton*)sender
{
    [buttonsState setObject:@YES forKey:@(sender.tag)];

    [sender setBackgroundColor:[UIColor lightGrayColor]];
    [sender setTintColor: [UIColor whiteColor]];
    [sender setTitle:@"Saved" forState:UIControlStateNormal];
    [sender addTarget:self
               action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
    sender.enabled = NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM