我在自定义单元格中有四个按钮,例如ans1,ans2,ans3,ans4,我想验证每个didselectrowatindexpath
indexpath.row
中的按钮我该怎么做?我还设置了按钮标签(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个按钮将取消选择等等... 在每个自定义单元格中
答案 0 :(得分:1)
- (IBAction)select:(id)sender {
UIButton* btn=(UIButton*)sender;
CustomCell* cel=(CustomCell*)[btn.superview superview];
if (cel.a1.tag==btn.tag) {
[cel.a1 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
}else{
[cel.a1 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
}
if (cel.a2.tag==btn.tag) {
[cel.a2 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
}else{
[cel.a2 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
}
if (cel.a3.tag==btn.tag) {
[cel.a3 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
}else{
[cel.a3 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
}
if (cel.a4.tag==btn.tag) {
[cel.a4 setImage:[UIImage imageNamed:@"active.png"] forState:UIControlStateNormal];
}else{
[cel.a4 setImage:[UIImage imageNamed:@"inactive.png"] forState:UIControlStateNormal];
}
}
答案 1 :(得分:1)
我看到你的工具现在不好。有办法得到它,但目前尚不清楚。所以我建议你应该将按钮操作的动作放到ContestQATableViewCell
的类中,并调用委托给控制器的表视图和doSomeThingWithit
首先创建委托:
@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
当你点击它时,你在Cell中添加动作,你调用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];
}
}
在控制器中你抓住并做你想做的事情:
- (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