How to save the selected state when you click on UIButton in iPhone?

I created three buttons and set the background image for the normal state and the selected state. When I press the button, one image in the selected state has changed. But when the table view is scrolled, the selected image is not saved (the previous selected cell), which means that it is suitable for a normal state.

My code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    UIImage *likeSelectedImage = [UIImage imageNamed:@"like.png"];

    //set image as background for button in the normal state

    [likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];

    [likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];

   [cell.contentView addSubview:likeBtn];
 }

The action of my button,

 -(void) likeAction : (id) sender
 {
     UIImage *likeSelectedImg = [UIImage imageNamed:@"like-selected.png"];

     UIImage *likeImg = [UIImage imageNamed:@"like.png"];

           if ([sender isSelected]) {
               [sender setImage:likeImg forState:UIControlStateNormal];
               [sender setSelected:NO];
               }else {
               [sender setImage:likeSelectedImg forState:UIControlStateSelected];
               [sender setSelected:YES];
          }

 }

SO my problem is that when I look at a table cell, the previously selected image state is not saved. Because the cellForRowAtIndex method called repeatedly when I was scrolling through the table. therefore it automatically installs "[likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];". How to avoid this problem? So please help me? Thank!

+3
4

- , UITableView . , , . , , , , .

, , tableView: cellForRowAtIndexPath:, - . , .

, , , , .

  • , NSArray, .
  • likeAction , (, ) NSArray
  • tableView: cellForRowAtIndexPath: , , NSArray .
+5

:

cell = [[[ToDoSummaryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

, .

. , "reuseidentifier" XCode IOS api.

,

+1

'reuseIdentifier' .

cell = [[[ToDoSummaryTableViewCell alloc] 
                     initWithStyle:UITableViewCellStyleDefault reuseIdentifier:.<identifier>] autorelease];
0
source

I will redo your code above. I hope I help you.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

       likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];

       UIImage *likeSelectedImage = [UIImage imageNamed:@"like.png"];

       //set image as background for button in the normal state

       [likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];
       [likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateHighlighted];

       [likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];

       [cell.contentView addSubview:likeBtn];
}
0
source

Source: https://habr.com/ru/post/1784738/


All Articles