UITableViewCell UIButton 选择 [英] UITableViewCell UIButton Selection

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

问题描述

如果有人可以帮助实现以下要求,那就太好了.在 tableviewcell 中,我有水平滚动视图,它将动态添加 uibuttons.用户可以从一行中选择多个按钮,但不能从不同的行中选择按钮.例如,如果我已经在第 1 行中选择了按钮(我应该能够选择一个或多个按钮),并且当我在第 2 行中点击按钮时,应取消选择第 1 行中的选定按钮,而应选择我在第 2 行中点击的按钮.

It would be great if someone can help to achieve the below requirement. In the tableviewcell, i have horizontal scrollview which will have uibuttons added dynamically. User can select multiple buttons from one row, but cannot select button from different rows. For e.g., If I have already selected buttons(i should be able to select one or more buttons) in row1, and when i tap button in row2, selected buttons in row1 should be deselected and the button which i tapped in row2 should be selected.

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

  cell.timeslotScrollView.contentSize = CGSizeMake(500, 0);
  cell.timeslotScrollView.scrollEnabled = YES;
  cell.timeslotScrollView.showsHorizontalScrollIndicator = NO;

  UIButton *button = [[UIButton alloc]init];
  button.frame = CGRectMake(0, 0, 68, 35);
  [button setTitle:@"abc" forState:UIControlStateNormal];

  button.layer.borderWidth = 2;
  button.layer.borderColor = [UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0].CGColor;
  button.layer.cornerRadius = 3;
  button.userInteractionEnabled = YES;
   [button setTitleColor:[UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0] forState:UIControlStateNormal];
  [button setTag:indexPath.row];
  [button addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside];

  UIButton *button1 = [[UIButton alloc]init];
  button1.frame = CGRectMake(button.frame.origin.x+68+buttonSpace, 0, 68, 35);
  [button1 setTitle:@"5 pm"  forState:UIControlStateNormal];
  [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  button1.layer.borderWidth = 2;
  button1.layer.borderColor = [UIColor grayColor].CGColor;
  button1.layer.cornerRadius = 3;
  button1.userInteractionEnabled = YES;
  [button1 setTag:indexPath.row];


  [button1 addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside];

  [cell.timeslotScrollView addSubview:button];
  [cell.timeslotScrollView addSubview:button1];

  }

  return cell;
}

-(void)didTap:(id)sender
{
  UIButton *pressedButton = (UIButton *)sender;
  if (pressedButton.tag != selectedButton ) {
    [sender setBackgroundColor:[UIColor greenColor]];

    selectedButton = pressedButton.tag;


  }
  else{
    [sender setBackgroundColor:[UIColor clearColor]];

  }
}

推荐答案

我会这样做:

- (void) tableView: (UITableView *) tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath {
     [[tableView cellForRowAtIndexPath: indexPath] deselectAllButtons];
}

现在每次取消选择一行时,它的所有按钮都将被取消选择,无论其他任何行发生什么.您仍然需要编写自己的 deselectAllButtons 方法,例如通过迭代 timeslotScrollView 的所有子视图.

Now everytime a row gets unselected, all its buttons will be deselected, no matter what happens on any of the other rows. You will still have to write your own deselectAllButtons method, e.g. by iterating over all the subviews of timeslotScrollView.

这篇关于UITableViewCell UIButton 选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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