UITableview scrolling change UIButton images, UITableview scrolling

What I do I have UITableview

and I added UIButton

as a custom view. I give a tag to each button and the tag I got in the action method. When I click the button, it changes images for the selected and unselected button, but when I scroll through it, it goes to normal.

This is my cell for row by index method

static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}

==================

//Here is the action method

-(void)followingButtonpressed:(id)sender
{
    NSLog(@"sender tag --%d",[sender tag]);
    UIButton *btnPly = (UIButton *)sender;
    if([btnPly isSelected])
    {
        [btnPly setSelected:NO];
        [btnPly setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
    }
    else
    {
        [btnPly setSelected:YES];
        [btnPly setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
    }
}

      

+3


source to share


4 answers


Note. This code creates a cell for each row of data (reuse cell)

You only need to change how to describe, may be helpful for you



NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

      

can solve your problem :)

+4


source


The reason your buttons are "reloading" is because tableView: cellForRowAtIndexPath is called multiple times (whenever the cell should be visible).

Each time this is called, you reinitialize your button and restore the image to the next 12.png (default state). Therefore, when scrolling, the buttons show reset.

You cannot rely on your own cell to keep the state, because the cell will get reset every time. You need to move your state to a different location (like an array instance variable). Then when you need to set up a new cell in tableView: cellForRowAtIndexPath, you can initialize it to the correct state (based on the array).

So, create an NSMutableArray instance variable called myStateArray (or whatever) to store your button states, then your cellForRowAtIndexPath should look bigger:



static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];

// -- Fetch the state from your array
BOOL buttonPressed = [[self.myStateArray objectAtIndex:indexPath.row] boolValue];

// -- Initialize the button state correctly
[followingButton setSelected:buttonPressed];
if (buttonPressed) {
    [followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
} else {
    [followingButton setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}



followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}

      

Then in your button click to save state:

-(void)followingButtonpressed:(id)sender
{
    // -- Save the state
    [self.myStateArray insertObject:[NSNumber numberWithBool:[btnPly isSelected]] atIndex:[sender tag]];

    NSLog(@"sender tag --%d",[sender tag]);
    UIButton *btnPly = (UIButton *)sender;
    if([btnPly isSelected])
    {
        [btnPly setSelected:NO];
        [btnPly setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
    }
    else
    {
        [btnPly setSelected:YES];
        [btnPly setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
    }
}

      

+2


source


As I understand from the code, you are creating a UIButton every time the UITableView asks for a cell. In both cases, if you create a new cell or reuse it. Then you create and add one more button each time according to the already created one. Create a button to go to create a cell if

So your method cellForRowAtIndexPath

should look like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell4";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
        followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
        [followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
        followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
        [cell.contentView addSubview:followingButton];
    }
    NSLog(@"row--%d",indexPath.row);
    followingButton.tag=indexPath.row;
    NSLog(@"followingButton.tag--%d",followingButton.tag);
    [self configureCellFollowing:cell forIndexPath:indexPath];
    return cell;
}

      

But this will not be a solvable solution to your problem. As you probably know, UITableView uses "reusable" cells to reduce system memory usage. It only uses the number of cells that need to be shown at the moment. Thus, in a table with 100 cells, about 10 will be created, and all of them will not store the correct state of all pressed / unpressed buttons. In order to achieve the correct behavior, you must abandon the use of tags and use some model logic. The easiest way is NSMutableArray where you will store the buttons. In the method, followingButtonpressed:

you will set the correct object to YES / NO, and in cellForRowAtIndexPath

you will read that value. Check the code below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell4";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
        followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
        [followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
        followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
        [cell.contentView addSubview:followingButton];

        BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
        [self setState:isSelected forButton:followingButton];
    }
    NSLog(@"row--%d",indexPath.row);
    followingButton.tag=indexPath.row;
    NSLog(@"followingButton.tag--%d",followingButton.tag);
    [self configureCellFollowing:cell forIndexPath:indexPath];
    return cell;
}

-(void)followingButtonpressed:(id)sender
{
    NSLog(@"sender tag --%d",[sender tag]);
    UIButton *btnPly = (UIButton *)sender;

    BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
    [statesArray replaceObjectAtIndex:btnPly.tag withObject:[NSNumber numberWithBool:!isSelected]];

    if(isSelected)
    {
        [self setState:NO forButton:btnPly];
    }
    else
    {
        [self setState:YES forButton:btnPly];
    }
}

- (void) setState:(BOOL)state forButton:(UIButton *)button
{
    if(state)
    {
        [button setSelected:NO];
        [button setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
    }
    else
    {
        [button setSelected:YES];
        [button setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
    }
}

      

Where statesArray

is

NSMutableArray *statesArray = [NSMutableArray new];

      

and you have to create and initialize it somewhere in your class. The number of objects in statesArray

must match the number of cells in the int tableView.

+1


source


This is because every time you scroll, this code calls

[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];

      

so now check

static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];

followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];

[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];

followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];

[self configureCellFollowing:cell forIndexPath:indexPath];
}
return cell;
}

      

0


source







All Articles