如何检测 tableview 单元格中的多个按钮,我的疑问是例如我在单元格中有 3 个按钮,如果我点击一个按钮,该按钮将改变颜色,如果我单击 indexpath.row=1 单元格按钮,该按钮将着色也需要换帮我
Best Answer-推荐答案 strong>
我是这样做的:
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button.tag = 100 + indexPath.row*total_buttons_in_a_row;
[button setTitle:[NSString stringWithFormat"%ld",(long)button.tag] forState:UIControlStateNormal];
[button addTarget:self actionselector(btnClicked forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1;
[button2 setTitle:[NSString stringWithFormat"%ld",(long)button2.tag] forState:UIControlStateNormal];
[button2 addTarget:self actionselector(btnClicked forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2;
[button3 setTitle:[NSString stringWithFormat"%ld",(long)button3.tag] forState:UIControlStateNormal];
[button3 addTarget:self actionselector(btnClicked forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button3];
return cell;
}
-(void)btnClickedUIButton *)sender{
id selectedButton = [self.view viewWithTag:sender.tag];
if ([selectedButton backgroundColor] == [UIColor redColor]) {
[selectedButton setBackgroundColor:[UIColor clearColor]];
}else{
[selectedButton setBackgroundColor:[UIColor redColor]];
}
}
total_buttons_in_a_row 是一个 Int 。在你的情况下定义它 viewDidLoad total_buttons_in_a_row=3
P.S - 根据您的需要设置按钮 CGRectMake 。
关于ios - 如何检测表格 View 单元格中的多个按钮,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29964147/
|