简体   繁体   English

滚动UITableView时,UITableViewCell选定按钮将更改为未选定状态

[英]UITableViewCell selected buttons change to unselected state upon scroll the UITableView

The button action is SongsSelectionSongs_Click . 按钮操作是SongsSelectionSongs_Click When I click this button, the button image changing, the button tap count is getting correct and after selected button images also changing, but when I scroll back and forth in the UITableView the button image seems to be randomly changing. 当我单击此按钮时,按钮图像更改,按钮点击计数变得正确,并且所选按钮图像也在变化,但是当我在UITableView来回滚动时,按钮图像似乎是随机变化的。

在此输入图像描述

This is my code: 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SongsTAbleViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SongsTAbleViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    btn_songSelect.tag = indexPath.row;
    lbl_songLabel.text = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:@"SongTitle"];
    lbl_artistLabel.text = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:@"SongArtist"];

    return cell;
}

-(IBAction)SongsSelectionSongs_Click:(id)sender
{
    UIButton *button = sender;
    CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.tblv_SongslisttableView];
    NSIndexPath *indexPath = [self.tblv_SongslisttableView indexPathForRowAtPoint:correctedPoint];
    NSLog(@"Button tapped in row %d",indexPath.row);

    SelectedAlbumUrl = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:@"SongUrl"];
    str_songtitle = [[arr_tablVArray objectAtIndex:indexPath.row] objectForKey:@"SongTitle"];

    if ([[button backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"add.png"]])
    {
        btn_songsShareButton.userInteractionEnabled = YES;
        [btn_songSelect setBackgroundImage:[UIImage imageNamed:@"remove.png"] forState:UIControlStateNormal];
        buttonStatus = buttonStatus +1;
        [btn_songsShareButton setImage:[UIImage imageNamed:@"share selected.png"] forState:UIControlStateNormal];
        }
    else
    {
        [btn_songSelect setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
        buttonStatus = 1;
        [btn_songsShareButton setImage:[UIImage imageNamed:@"share unselected.png"] forState:UIControlStateNormal];
    }
}

You are not doing anything within your cellForRowAtIndexPath to select or deselect image. 您没有在cellForRowAtIndexPath执行任何操作来选择或取消选择图像。 When you reuse a cell, it doesn't change the state of the cell unless you explicitly tell it to in cellForRow . 当您重用一个单元格时,除非您在cellForRow明确告知它,否则它不会更改单元格的状态。 Therefore, it will either reuse a selected or deselected cell (whatever is the first available reusable cell) and put that on the screen as-is. 因此,它将重用一个选定或取消选择的单元格(无论是第一个可用的可重用单元格),并按原样将其放在屏幕上。

To fix this issue, you need logic in your cellForRowAtIndexPath method to either select or deselect the image based on what is appropriate. 要解决此问题,您需要在cellForRowAtIndexPath方法中使用逻辑,以根据适当的选择选择或取消选择图像。


In general, if your problem has anything to do with "my cells don't show up right when scrolling" odds are you're not reusing your cells properly. 一般来说,如果你的问题与“滚动时我的单元格没有正确显示”有关,那么你可能没有正确地重复使用你的单元格。

EDIT: in response to your comment, no , I will not rewrite your code. 编辑:回应你的评论, ,我不会重写你的代码。 But I will give you some direction. 不过,我会给你一些指导。

I would recommend keeping an additional key/value on your arr_tablVArray that will track whether or not the "share" should be enabled or disabled (I would suggest a bool value). 我建议在你的arr_tablVArray上保留一个额外的键/值,它将跟踪是否应该启用或禁用“共享”(我建议使用bool值)。 This would make it so that you could check whether or not the "share" is enabled/disabled by checking a bool instead of checking the contents of the button's image in your IBAction method. 这样可以通过检查bool来检查是否启用/禁用“共享”,而不是在IBAction方法中检查按钮图像的内容。

This info would now be available in your cellForRowAtIndexPath method as well, and you could check the value for the current record in arr_tablVArray and set your images accordingly just like you set your lbl_songLabel and lbl_artistLabel . 此信息现在也可以在您的cellForRowAtIndexPath方法中使用,您可以检查arr_tablVArray当前记录的值,并相应地设置图像,就像设置lbl_songLabellbl_artistLabel

//Try It, it's Working Fine //试试吧,它运作良好

pragma .h File pragma .h文件

NSMutableArray * rowIdArray;

pragma .M File pragma .M文件

@synthesize rowIdArray;

- (void)viewDidLoad
  {
    rowIdArray=[[NSMutableArray alloc]init];
  }

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   return 1;
 }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
   return [NamesArray count];
 }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   static NSString *CellIdentifier = @"Cell";
   ViewControllerCell *cell = (ViewControllerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if(cell == nil)
     {
        NSArray *nib;
        nib = [[NSBundle mainBundle] loadNibNamed:@"ViewControllerCell" owner:self options:nil];


        cell = [nib objectAtIndex:0];
     }
// Configure the cell...
  cell.nameslbl.text = [NamesArray objectAtIndex:indexPath.row];

  cell.nameBtn.tag=indexPath.row;
  [cell.nameBtn addTarget:self action:@selector(NamesClick_Tapped:) forControlEvents:UIControlEventTouchUpInside];

   NSString *a=[NSString stringWithFormat:@"%d", indexPath.row];
   NSString *b=[[NSString alloc]init];

  for (int i=0;i<[rowIdArray count];i++)
   {
       b=[rowIdArray  objectAtIndex:i];

        if ([a isEqualToString:b])
         {

           UIImage *buttonImage = [UIImage imageNamed:@"star_selected.png"];
           [cell.nameBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
          }

       }

      return cell;
}

-(IBAction)NamesClick_Tapped:(id)sender
  {

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.NameTableView];

    NSIndexPath *indexPath = [self.NameTableView indexPathForRowAtPoint:buttonPosition];

    NSString *rowIdStr = [NSString stringWithFormat:@"%d", indexPath.row];

    if(![rowIdArray containsObject:rowIdStr])
     {

       [rowIdArray addObject:rowIdStr];

     }else{

      [rowIdArray removeObject: rowIdStr];

     }
    [self.NameTableView reloadData];
 }

when you reuse a cell where the button has been already set, the same button appears with the previously set image. 当您重复使用已设置按钮的单元格时,会出现与先前设置的图像相同的按钮。 Instead of creating a new button every time you need a cell, you should just be resetting the state of an existing button. 您不必每次需要单元格时都创建新按钮,而应该重置现有按钮的状态。 This link might help you. 此链接可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动UITableView时插入UITableViewCell中的UIButton的重复选定状态 - Duplicated selected state of UIButton inserted in UITableViewCell when Scroll UITableView 滚动UITableView时UITableViewCell的位置更改 - UITableViewCell position change while scroll UITableView 滚动时Swift UITableViewCell按钮状态更改 - Swift UITableViewCell Button State Change on Scroll 无法更改UITableViewCell中按钮的选定状态图像 - unable to change selected state image of button in UITableViewCell 更新 UITableView 滚动上的 UITableViewCell - Update UITableViewCell on UITableView scroll 滚动UITableView时未选中单选按钮 - Radio buttons get unselected while scrolling UITableView 在 Swift 中的 UITableView 中设置已选择/未选择的视图颜色 - Set Selected/ Unselected view color in UITableView in Swift UITableView didDeselectRowAtIndexPath单元格在未选中时返回不在选中时返回 - UITableView didDeselectRowAtIndexPath Cell returns on unselected Not on Selected 在滚动之前,UITableViewCell按钮不会显示选中或取消选中状态 - UITableViewCell button doesn't show selected or deselected state until scroll 重置UITableViewCell无法正常工作/滚动后未选中的单元格显示为选中状态 - Resetting UITableViewCell Not working properly / Unselected Cells appears selected after scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM