搜索后Tableview按钮选择错误 [英] Tableview button selection wrong after search

查看:25
本文介绍了搜索后Tableview按钮选择错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 中有一个 UIButton.当一个 UIButton 被点击时,我将被点击的按钮标签保存在数组中.因此,当我使用 UISearchControllerUITableView 中搜索时,我得到了所需的输出,但在错误的单元格中选择了所选按钮.

I have a UIButton in the UITableViewCell. When a UIButton is tapped, I'm saving the tapped button tag in the array. So when I search within the UITableView using the UISearchController, I'm getting the desired output, but the selected button is selected within the wrong cell.

图片 1 - 按钮被选中且在搜索前

Pic 1 - Button selected and before search

图片 2 - 搜索后

在图 1 中,我选择了属于房间 1"的按钮,但在搜索后,如您在图 2 中看到的,输出是正确的,但不应选择按钮.如何解决这个问题?

In the pic 1, I have selected the button that belongs to "room 1", but after the search, as you can see in the pic 2, the output is correct, but the buttons shouldn't be selected. How to solve this issue?

---- 已编辑----实施

---- Edited---- Implementation

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (shouldShowSearchresult) {
    return searchArray.count;
  }
  else
  {
    return roomNameArray.count;
  }

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *cellIdentifier = @"roomCells";
  cell = (roomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (shouldShowSearchresult) {
    cell.roomName.text = [searchArray objectAtIndex:indexPath.row];
    // cell.seatingCapacity.text = [NSString stringWithFormat:@"Seats %@ people",[seatingCapacityArray objectAtIndex:indexPath.row]];
    cell.approval.text = @"Approval not required";
  }
  else
  {
    cell.roomName.text = [roomNameArray objectAtIndex:indexPath.row];
    // cell.seatingCapacity.text = [NSString stringWithFormat:@"Seats %@ people",[seatingCapacityArray objectAtIndex:indexPath.row]];
    cell.approval.text = @"Approval not required";

  }

    for (UIButton *slotButton in cell.timeslotScrollView.subviews) {
      [slotButton removeFromSuperview];
    }
    UIButton *slotButton;
  cell.timeslotScrollView.contentSize = CGSizeMake(500, 0);
  cell.timeslotScrollView.scrollEnabled = YES;
  cell.timeslotScrollView.showsHorizontalScrollIndicator = NO;
  int buttonSpace = 10;
  int buttonWidth = 68;
  int buttonHeight = 35;
  int yPosition = 0;
  float xPosition = 0;



    for (int i=0; i<timeSlotArray.count; i++) {
      slotButton = [[UIButton alloc]init];
      slotButton.frame = CGRectMake(xPosition, yPosition, buttonWidth, buttonHeight);
      [slotButton setTitle:[NSString stringWithFormat:@"%@", timeSlotArray[i]] forState:UIControlStateNormal];
      [slotButton setTitleColor:[UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0] forState:UIControlStateNormal] ;
      slotButton.layer.borderWidth = 2;
      slotButton.layer.borderColor = [UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0].CGColor;
      slotButton.layer.cornerRadius = 3;
      slotButton.tag = i;



      slotButton.userInteractionEnabled = YES;
      [slotButton addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside];
      xPosition = slotButton.frame.origin.x+buttonWidth+buttonSpace;



      if (cell.timeslotScrollView.subviews.count < timeSlotArray.count)
      {
        [cell.timeslotScrollView addSubview:slotButton];
      }

    }

  NSArray *tempArray = cell.timeslotScrollView.subviews;
  NSLog(@"%ld",tempArray.count);

  return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cello forRowAtIndexPath:(NSIndexPath *)indexPath{


  if (indexPath.row==tappedButtonRowIndex)
  {

    for (int i = 0; i <cell.timeslotScrollView.subviews.count; i++)
    {
      UIView *view = [cell.timeslotScrollView.subviews objectAtIndex:i];
      if ([view isKindOfClass:[UIButton class]])
      {
        UIButton *slotTempBtn = (UIButton *)view;
        BOOL btnFound = false;
        for (NSString *index in buttonSelectedArray)
        {
          if ([index integerValue]== slotTempBtn.tag)
          {
            btnFound = true;
            break;
          }
        }
        if (btnFound) {
          [slotTempBtn setBackgroundColor:[UIColor greenColor]];
        }
        else
        {
          [slotTempBtn setBackgroundColor:[UIColor whiteColor]];
        }
      }

    }
  }
  else
  {
    for (UIView *view in cell.timeslotScrollView.subviews)
    {
      if ([view isKindOfClass:[UIButton class]])
      {
        UIButton *slotTempBtn = (UIButton *)view;
        [slotTempBtn setBackgroundColor:[UIColor whiteColor]];
      }
    }
  }




  cell.slotButtonDelegate=self;

}

搜索实现

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
  [self updateFilteredContentWithSearchText:searchController.searchBar.text];

  [_roomTableView reloadData];
}

- (void)updateFilteredContentWithSearchText:(NSString*)searchText
{
  [searchArray removeAllObjects];
  for (NSString *room in roomNameArray)
  {
    NSRange nameRange = [room rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if (nameRange.location != NSNotFound)
    {
      [searchArray addObject:room];
    }
  }
  self.roomTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  if (searchBar.text.length >= 1) {
    shouldShowSearchresult = true;
     }
  else
  {
    shouldShowSearchresult = false;
  }
  [_roomTableView reloadData];

}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
  shouldShowSearchresult = false;
  [_roomTableView reloadData];
}

推荐答案

由于这里重复使用 tableview 单元格,因此您需要创建一个条件并在那里检查条件是否为真则选择按钮,否则按钮是正常的,因此下次单元格时将重用它将按照数组数据正确显示.在您的 tableView cellForRow 方法

As the tableview cell is reused here you need to make one condition and check there if condition is true then button is selected otherwise button is normal so when next time cell is going to reuse it will display proper as per array data. check in the condition in your tableView cellForRow method

if yourArray.count > 0 {
    //check your array and set cell's button selection as per id or tag
}
else { 
   //set all cells all the button unselected
}

这篇关于搜索后Tableview按钮选择错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆