ios - 如何使用 didselectrowatindexpath 中的图像验证单选按钮?

标签 ios uitableview

我在自定义单元格中有四个按钮,例如 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

的 Controller

首先创建委托(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/

相关文章:

ios - 如何使用 Swift 在 CustomCell 中将 inputAccessoryView 实现到 UITextField?

ios - Swift PhysicsBody 不受 Impulse 影响

iOS 位码 - 安全问题

ios - 在桌面上开发的移动应用程序如何工作?

3.5寸模拟器显示时iOS UITableView高度问题

ios - 如何通过标题中的按钮按下来确定 UITableView 中的当前部分

ios - UITableViewCell 副标题不显示

ios - 多个模态视图点击即可关闭

ios - Firebase 回调不会触发

ios - 可折叠表格 View 错误