【问题标题】:UITableViewCell UIButton Selection [duplicate]UITableViewCell UIButton选择[重复]
【发布时间】:2016-12-15 17:41:14
【问题描述】:

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

-(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]];

  }
}

【问题讨论】:

  • 你有代码吗?如果是这样,请发布它。如果没有,请尝试解决您的问题,如果仍然无法解决,请返回。谢谢
  • @CalebKleveter 我已经更新了我的问题以及我尝试过的代码
  • 你有什么问题?
  • 如果我已经在 row1 中选择了按钮(我应该能够选择一个或多个按钮),并且当我点击 row2 中的按钮时,应该取消选择 row1 中的选定按钮并且我点击的按钮在 row2 应该被选中。
  • 其实另一个问题是这个问题的重复。

标签: ios objective-c uitableview uibutton xcode8


【解决方案1】:

我会尝试不同的方法。我会将 UITableViewCell 子类化并在单元实现文件中添加按钮。然后我会在这个单元格中添加一个方法来触发按钮的选择/取消以及您需要执行的任何其他操作。

此时,在 cellForRowAtIndexPath 委托中,您唯一需要关心的是调用单元格中的方法来执行选择取消选择。

【讨论】:

  • 即使你继承了 uitableviewcell ,你也必须单独编写按钮操作方法,我相信所有的选择/取消选择都会发生在这个方法中。所以无论你是否子类化都没有太大区别。
【解决方案2】:

我会这样做:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 2017-04-07
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多