Tableviewcell按钮

时间:2016-12-17 10:36:35

标签: objective-c uitableview uibutton xcode8

我有tableview,我将tableviewcell子类化。单元格中有一个水平滚动视图,我将动态按钮添加到滚动视图。

enter image description here

我的要求: 1.当我第一次点击row0上的按钮时,我需要为点击按钮设置不同的BG颜色,并在数组中添加按钮索引。当我第二次点击相同的按钮时,我需要清除轻敲按钮的BG并从阵列中删除按钮索引 - 我能够实现这一点 2.假设我在row0中选择了两个按钮,当我尝试点击row1上的某个按钮时,我应该清除row0中所有选中按钮的BG颜色,清除数组并设置按下的按钮的BG颜色。 row2并在同一个数组中添加此按钮索引 - 我遇到了很大的问题。

在上面附图中,我在“房间1”中选择了“测试0”和“测试1”按钮。现在当我点击“房间2”中的“测试0”或“测试1”按钮时,我应该清除“房间1”中的所有选定按钮,并在“房间2”中选择/设置轻拍按钮的BG颜色。

//更新了代码     Mysolution:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      static NSString *cellIdentifier = @"roomCells";

      cell = (roomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  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<=1; i++) {
    slotButton = [[UIButton alloc]init];
    slotButton.frame = CGRectMake(xPosition, yPosition, buttonWidth, buttonHeight);
    [slotButton setTitle:[NSString stringWithFormat:@"Test %d",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 < numberOfItems)
    {
      [cell.timeslotScrollView addSubview:slotButton];
    }
  }

      }

      return cell;
    }


Updated Action method:

-(void)didTap:(id)sender
{
  static int buttonTapCount = 0;
  buttonTapCount++;
  NSMutableArray *toDelete = [NSMutableArray new];
  NSMutableArray *toAdd = [NSMutableArray new];
  UIButton *pressedButton = (UIButton *)sender;
  CGPoint touchPoint = [pressedButton convertPoint:CGPointZero toView:self.roomTableView];
  NSIndexPath *indexOfButton = [self.roomTableView indexPathForRowAtPoint:touchPoint];
  NSInteger buttonIndex = pressedButton.tag;
  NSString *buttonIndexString = [NSString stringWithFormat:@"%ld",(long)buttonIndex];

if (buttonTapCount==1)
{
tappedButtonRowIndex = indexOfButton.row;
[toAdd addObject:buttonIndexString];
[pressedButton setBackgroundColor:[UIColor greenColor]];
}
else if(buttonTapCount>=1)
{
  if (tappedButtonRowIndex==indexOfButton.row)
  {
      if (buttonSelectedArray.count==0)
      {
      [toAdd addObject:buttonIndexString];
      [pressedButton setBackgroundColor:[UIColor greenColor]];
      }
      else
      {
      isButtonAlreadySelected = false;
      for (NSString  *value in buttonSelectedArray)
      {
        if ([buttonIndexString isEqualToString:value])
        {
        [toDelete addObject:value];
        [pressedButton setBackgroundColor:[UIColor clearColor]];
        isButtonAlreadySelected = true;
        }
      }
      }
  }
  else
  {
  if (buttonSelectedArray.count==0)
      {
      [toAdd addObject:buttonIndexString];
      [pressedButton setBackgroundColor:[UIColor greenColor]];
      tappedButtonRowIndex = indexOfButton.row;
      isButtonAlreadySelected=true;
      }
      else
      {
        isButtonAlreadySelected=true;

      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"Please deselect the previous selected slots" preferredStyle:UIAlertControllerStyleAlert];

      [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil]];

      [self presentViewController:alertController animated:YES completion:nil];

  }
  }
}

0 个答案:

没有答案