2015-12-09 16 views
3

我有自定義單元格中的四個按鈕,如ans1,ans2,ans3,ans4,我想驗證didselectrowatindexpath中的每個indexpath.row中的按鈕,我該怎麼做?我還設置了按鈕標籤(1,2,3,4)? 這裏是我的cellforatindexpath代碼如何驗證單選按鈕與didselectrowatindexpath中的圖像?

-(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剩餘3buttons點擊將被取消 如果我在ANS2剩餘3buttons單擊每個自定義單元格將被取消等等...

+0

可能的複製[檢測其UIButton的是在一個UITableView壓制(http://stackoverflow.com/questions/1802707/detecting-which -uibutton-was-pressed -a-uitableview) – evnaz

+0

這是一些如何複製到您提供的鏈接_evnaz_,但_Mangi_以其他方式使用四個按鈕在一個單元格中執行此操作。如果我誤解了某些內容,請糾正。 –

回答

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]; 
    } 

} 
+0

我有四個按鈕。我如何管理? –

+0

@MangiReddy添加普通的IBAction到所有四個按鈕,即 - (IBAction)選擇:(id)發件人,然後得到像我更新的答案- –

+0

當我在ContestQAViewController.m這個代碼按鈕它說未申報的標識符btn超級查看。 –

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 

你在細胞中添加行動時,使用者開關它你電話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

+0

抱歉,我已經創建了ContestQACustomCell(.h和.m),我還爲標籤創建了屬性n按鈕 –

+0

您可以按照我的方式做。它只是演示,你可以拖動出口和動作,並以同樣的方式。 –

+0

謝謝你的回覆:) –