2017-02-27 86 views
0

我的應用程序有多個選擇題和答案類型屏幕。一組單選按鈕在同一行中選擇一個單選按鈕,它影響另一行 - 目標-c

爲此,我設計了UITableView屏幕,即在一行中,我用四個單選按鈕拍攝了四個答案。 這裏我確切的問題是,如果當我在一行中選擇一個單選按鈕時,它會自動選擇另一行以及我之前選擇的行。在row1中的示例我已經選擇了option2單選按鈕,在這裏它也自動選擇另一行option2。

我已經嘗試了下面的代碼,請幫我對此。 在此先感謝。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
 
    
 
    cell = [_tableViewRef dequeueReusableCellWithIdentifier:@"cell"]; 
 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
 
    NSString *rowNumber = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1]; 
 
    [[cell questionLbl] setText:[NSString stringWithFormat:@"%@. %@",rowNumber,@"Provide your own fixed background behind the UITableView?"]]; 
 
    
 
    
 
    [cell.option1RadioBtn addTarget:self 
 
       action:@selector(radioBtnn1Action:) 
 
     forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option2RadioBtn addTarget:self 
 
          action:@selector(radioBtnn2Action:) 
 
        forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option3RadioBtn addTarget:self 
 
          action:@selector(radioBtnn3Action:) 
 
        forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option4RadioBtn addTarget:self 
 
          action:@selector(radioBtnn4Action:) 
 
        forControlEvents:UIControlEventTouchUpInside]; 
 
     return cell; 
 
}

在單選按鈕的動作我已經試過這段代碼:

-(void)radioBtnn1Action:(UIButton*)sender 
 
{ 
 
    
 
    CGPoint center= sender.center; 
 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef]; 
 
    NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint]; 
 
    cell = [self.tableViewRef cellForRowAtIndexPath:indexPath]; 
 
    
 

 

 
    if ([cell.option1RadioBtn isSelected]) { 
 
     [cell.option1RadioBtn setSelected:YES]; 
 
     
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 

 
    } 
 
    else 
 
    { 
 
     [cell.option1RadioBtn setSelected:YES]; 
 
     
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 

 
    } 
 

 
    
 
    
 
    
 
} 
 
-(void)radioBtnn2Action:(UIButton*)sender 
 
{ 
 
    
 
    CGPoint center= sender.center; 
 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef]; 
 
    NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint]; 
 
    cell = [self.tableViewRef cellForRowAtIndexPath:indexPath]; 
 
    
 

 
    if ([cell.option2RadioBtn isSelected]) { 
 
     [cell.option2RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    else 
 
    { 
 
     [cell.option2RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    
 
    
 
} 
 
-(void)radioBtnn3Action:(UIButton*)sender 
 
{ 
 
    
 
    CGPoint center= sender.center; 
 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef]; 
 
    NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint]; 
 
    cell = [self.tableViewRef cellForRowAtIndexPath:indexPath]; 
 
    
 

 
    if ([cell.option3RadioBtn isSelected]) { 
 
     [cell.option3RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    else 
 
    { 
 
     [cell.option3RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option4RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    
 
    
 
} 
 
-(void)radioBtnn4Action:(UIButton*)sender 
 
{ 
 
    
 
    CGPoint center= sender.center; 
 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef]; 
 
    NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint]; 
 
    cell = [self.tableViewRef cellForRowAtIndexPath:indexPath]; 
 
    if ([cell.option4RadioBtn isSelected]) { 
 
     [cell.option4RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    else 
 
    { 
 
     [cell.option4RadioBtn setSelected:YES]; 
 
     
 
     [cell.option1RadioBtn setSelected:NO]; 
 
     [cell.option2RadioBtn setSelected:NO]; 
 
     [cell.option3RadioBtn setSelected:NO]; 
 
     
 
    } 
 
    
 
    
 
}

+0

您應該爲單選按鈕使用標籤。像這樣:=> option1RadioBtn.tag = 1等等。而radioBtnn1Action函數驗證如果option1RadioBtn.tag == 1這樣的東西。 – iParesh

+0

嘿@ iParesh感謝您的回覆,請你在這裏發佈一些代碼片段。 – Sajida

+0

你需要創建一個nsmutablearray默認情況下添加4個單選按鈕值0.hwen用戶可以選擇任何一個設置1值用於特定的單選按鈕索引值因爲這個問題是tableview可重複使用的單元格問題 – iOS

回答

0

有很多地區在你的代碼,你可以改善和小事情會導致這種行爲。我的發佈代碼與修改。請用我的代碼替換你的代碼塊。代碼中的問題是您有全局聲明該單元格並採用全局聲明的表格視圖的引用。在按鈕點擊檢測單元的情況下,我希望你做的最有效的改進。你不需要這麼多的代碼來實現這一點。開始吧。

爲了保持選擇狀態,我們將使用數組sectionData。

將此代碼添加到加載函數。

for (int index = 0; index < 10; index ++) {//taken 10 because you are using only 10 rows 
    [sectionData addObject:@"0"]; 
} 

和更換你的代碼下面的函數完全

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 
{ 
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
 
    NSString *rowNumber = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1]; 
 
    [[cell questionLbl] setText:[NSString stringWithFormat:@"%@. %@",rowNumber,@"Provide your own fixed background behind the UITableView?"]]; 
 

 
    [cell.option1RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"1"])]; 
 
    [cell.option2RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"2"])]; 
 
    [cell.option3RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"3"])]; 
 
    [cell.option4RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"4"])]; 
 

 
    cell.option1RadioBtn.indexPath = indexPath; 
 
    cell.option2RadioBtn.indexPath = indexPath; 
 
    cell.option3RadioBtn.indexPath = indexPath; 
 
    cell.option4RadioBtn.indexPath = indexPath; 
 

 
    [cell.option1RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option2RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option3RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside]; 
 
    [cell.option4RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside]; 
 
     return cell; 
 
}

您可以看到使用變量的引用只在函數內部我。我只將目標作爲1個功能。現在你需要做的技巧是創建一個UIButton的子類並將其命名爲CustomButton。另請參閱上文中我已將indexPath分配給按鈕屬性的行。 不要忘記更改UITableViewCell的故事板和類文件中Outlet和Element的類。

自定義按鈕的.h文件。

@interface CustomButton : UIButton 

@property (nonatomic, strong) NSIndexPath *indexPath; 

@end 

現在您的目標函數將如下所示。

- (void)radioBtnnAction:(CustomButton*)sender 
 
{ 
 
    UITableViewCell *cell = [self.tableViewRef cellForRowAtIndexPath:sender.indexPath]; 
 
    
 
    [sectionData replaceObjectAtIndex:sender.indexPath.row withObject:((sender == cell.option1RadioBtn)[email protected]"1":(sender == cell.option2RadioBtn)[email protected]"2":(sender == cell.option3RadioBtn)[email protected]"3":@"4")]; 
 

 
    [cell.option1RadioBtn setSelected:(sender == cell.option1RadioBtn)]; 
 
    [cell.option2RadioBtn setSelected:(sender == cell.option2RadioBtn)]; 
 
    [cell.option3RadioBtn setSelected:(sender == cell.option3RadioBtn)]; 
 
    [cell.option4RadioBtn setSelected:(sender == cell.option4RadioBtn)]; 
 
}

我希望我已經包括了一切。嘗試一次。

+0

我錯過了我的代碼中的東西。我編輯了答案。請使用cellForRowAtIndexPath的最新代碼塊 –

+0

嗨,謝謝你,但在這裏[cell.option1RadioBtn setIndexPath:indexPath]; 顯示了一些錯誤,如* UButton的* no visible @ interface聲明選擇器'setIndexpath'* – Sajida

+0

請參考答案中的粗體行。我提到你要將這些按鈕的類改爲CustomButton。 –

0

看起來您已經擁有一個自定義單元格。

而不是在單元上暴露標籤questionLbl。 讓單元格採用問題模型。 該模型可以像

類QuestionModel 物業問題 屬性選擇

你的電池有一個屬性= questionModel 和手柄的按鈕選擇。因此,當您使用單元格屬性設置器分配問題模型時,也可以設置按鈕狀態。在更新問題模型選擇屬性的單元格中有按鈕目標/操作調用方法。

您在tableView源文件中設置了cell.question,而不是直接設置標籤。

底線是,單元格應該處理模型而不是viewController。它只是將模型傳遞給單元格。

+0

在重新閱讀您的問題時,問題模型應該包含答案,因爲屬性甚至可以作爲數組。問題的關鍵在於,QuestionModel應該具有顯示狀態並記錄狀態變化所需的所有信息。 – Taun

相關問題