【发布时间】: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