如何检测tableview单元格中的多个按钮,我的疑问是示例我在单元格中有3个按钮如果我点击一个按钮该按钮将改变颜色,如果我单击indexpath.row = 1单元格按钮该按钮将着色还需要改变帮助我
答案 0 :(得分:1)
使用section&组合为每个按钮分配标签。排..
当分配给按钮的方法被调用时,使用'%' &安培; ' /'进一步操纵。
如果您在执行方面遇到任何困难,请告诉我。
答案 1 :(得分:1)
我确实喜欢这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 action:@selector(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 action:@selector(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 action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button3];
return cell;
}
-(void)btnClicked:(UIButton *)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
。
答案 2 :(得分:0)
到你的CustomCell.h&在#import下面添加此代码。
@protocol CustomCellDelegate <NSObject>
- (void)buttonActionwith :(NSIndexPath *)indexPath;
@end
然后在@interface CustomCell:UITableViewCell
下面添加此代码//Manual Properties
@property (strong, nonatomic) NSIndexPath *buttonIndexPath;
//Delegate
@property (nonatomic, weak) id <CustomCellDelegate> delegate;
使用此NSIndexPath是为了识别选择哪个单元用户。
然后转到CustomCell.m&amp;在您的按钮IBAction方法中添加此代码。
[self.delegate buttonActionwith:self.buttonIndexPath];
这行代码的含义是,当用户TouchUpIns使用委托方法调用单元格buttonAction中的Button时,如果你在UITiewView的UIViewController中设置了这个CustomCellDelegate,那个委托方法也将在该ViewController中调用。
现在转到MainViewController.h&amp;加 CustomCellDelegate &安培;之后会是这样的,
@interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate>
在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中创建自定义UITableViewCell时,不要忘记在返回单元格之前添加此行代码;
//Set Cell Values Here
cell.delegate = self; //Setting delegate to self
cell.buttonIndexPath = indexPath;
修改后的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
将如下所示,
static NSString *MyIdentifier = @"MyIdentifier"; //Set Identifier for cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell
if (cell == nil) { //Check cell is nill or not
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self options:nil]; //if cell is nil add CustomCell Xib
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
//Set Cell Values Here
cell.buttonIndexPath = indexPath
return cell;
最后在MainViewController.m中添加此方法
#pragma mark - SwipeableCellDelegate
- (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method
NSLog(@"Button Clicks at index %ld",(long)indexPath.row);
}
这是在Cell中添加单个按钮的方法。您可以使用此方法向单元格添加更多按钮。