我在自定义单元格中有四个按钮,例如 ans1、ans2、ans3、ans4,因为我想验证每个 indexpath.row
中的 didselectricwatindexpath
中的按钮,我怎样才能去做?我还设置了按钮标签(1,2,3,4)?
这是我的 cellforatindexpath 代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.question.text = [getContestQArray objectAtIndex:indexPath.row];
NSString *str1 = [getAnswer1Array objectAtIndex:indexPath.row];
NSString *str2 = [getAnswer2Array objectAtIndex:indexPath.row];
NSString *str3 = [getAnswer3Array objectAtIndex:indexPath.row];
NSString *str4 = [getAnswer4Array objectAtIndex:indexPath.row];
[cell.answer1 setTitle:str1 forState:UIControlStateNormal];
[cell.answer2 setTitle:str2 forState:UIControlStateNormal];
[cell.answer3 setTitle:str3 forState:UIControlStateNormal];
[cell.answer4 setTitle:str4 forState:UIControlStateNormal];
return cell;
}
如果我点击 ans1 剩余的 3 个按钮将被取消选择 如果我点击 ans2 剩余的 3 个按钮将被取消选择等等...... 在每个自定义单元格中
最佳答案
我发现你现在的工具不太好。有办法得到但不清楚。因此,我建议您应该将按钮操作的操作放入 ContestQATableViewCell
类中,并将委托(delegate)调用回具有 TableView 和 doSomeThingWithit
首先创建委托(delegate):
@protocol ContestCellDelegate <NSObject>
@required
- (void)didTapButtonAtIndexpath:(NSIndexPath *)indexPath posision: (NSInteger)position;
@end
@interface ContestQATableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *question;
@property (weak, nonatomic) IBOutlet UIButton *answer1;
@property (weak, nonatomic) IBOutlet UIButton *answer2;
@property (weak, nonatomic) IBOutlet UIButton *answer3;
@property (weak, nonatomic) IBOutlet UIButton *answer4;
@property (strong, nonatomic) NSIndexPath *indexPath;
@property (weak, nonatomic) id<ContestCellDelegate>delegate;
//- (void)configCellWithQuestion:()
@end
当用户点击单元格时,您可以在单元格中添加操作,您可以回调delgate:
- (IBAction)answer1IsTapped:(id)sender {
if (self.delegate) {
[self.delegate didTapButtonAtIndexpath:self.indexPath posision:1];
}
}
- (IBAction)answer2IsTapped:(id)sender {
if (self.delegate) {
[self.delegate didTapButtonAtIndexpath:self.indexPath posision:2];
}
}
- (IBAction)answer3IsTapped:(id)sender {
if (self.delegate) {
[self.delegate didTapButtonAtIndexpath:self.indexPath posision:3];
}
}
- (IBAction)answer4IsTapped:(id)sender {
if (self.delegate) {
[self.delegate didTapButtonAtIndexpath:self.indexPath posision:4];
}
}
在 Controller 中,您可以捕获并执行您想要的操作:
- (void)didTapButtonAtIndexpath:(NSIndexPath *)indexPath posision:(NSInteger)position {
//You can get index postion and do anything here
NSLog(@"%ld %ld", (long)indexPath.row, (long)position);
}
更多详细信息,您可以获取此代码并将其应用于您的项目:Demo Code
关于ios - 如何使用 didselectrowatindexpath 中的图像验证单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34178879/