2016-10-03 40 views
0

NSMutableArrayを持つUITableViewに人の名前をセルのラジオボタンと共に表示していますが、私が知りたいのは、ナビゲーションバーの[クリック]ボタンをクリックするとclearAllButtonという名前が付けられ、すべてのラジオボタンが選択されていることを意味します。ユーザーがclearAllButtonをクリックしたときのすべてのradioButton)。私は新しい開発者です。どんな助力も大歓迎です! clearAllButtonActionで以下のコードを試しましたが、機能しません。iOSでボタンをクリックするとラジオボタンの選択を解除する方法

- (IBAction)clearAllBtnActn:(id)sender { 
 
    
 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
 

 
    if([btnRadio isSelected]); 
 
    { 
 
     [btnRadio setSelected:NO]; 
 
    } 
 
    
 
}

+2

の選択を解除し、あなたの上記のコードで起こった何ウルのコード... –

+0

を示して? – Abha

+0

もう一度私の質問を確認してください。#Karthik – hani

答えて

0

まず、基礎となるデータソースは、テーブルビューのセルを駆動することができ、そこから(例えばNSArrayMyItemのオブジェクト)を必要とします。

あなたMyItemオブジェクトは、次のようになります。

@interface MyItem() 

@property (nonatomic, strong) NSString *title; 
@property (nonatomic, readwrite) BOOL selected; 

@end 

あなたのビューコントローラにデータソースmyItemsを作成します:あなたのビューコントローラで

@interface MyViewController() 

@property (nonatomic, weak) IBOutlet UITableView *tableView; 
@property (nonatomic, strong) NSArray *myItems; 

@end 

を、あなたの細胞を作成します。

@implementation MyViewController 

... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    MyButtonTableViewCell *cell = [tableView [email protected]"ButtonCell" forIndexPath:indexPath]; 
    MyItem *itemForCell = myItems[indexPath.row]; 

    cell.titleLabel.text = itemForCell.title; 
    cell.radioButton.selected = itemForCell.selected; 

    return cell; 
} 

... 

@end 

あなたの "明確な"方法は、これは

- (IBAction)clearAllBtnActn:(id)sender { 

    // Deselect all items 
    for (MyItem *item in self.myItems) 
     item.selected = NO; 

    [self.tableView reloadData]; 
} 
+0

よろしくお願いします。 – hani

+0

テーブルの各セルに「何か」(アイテム)に関するデータが表示されています。実際の品目は、テーブルビューとは完全に別のモデルで保持する必要があります。あなたはMyItemと呼ばれるクラスを作成して、単一のアイテムを表現する必要があります。次に、すべてのアイテムの実際のデータを保持する配列を作成します。 MVC(https://en.wikipedia。org/wiki/Model-view-controller)とUITableView(https://developer.apple.com/reference/uikit/uitableview?language=objc) – norders

+0

しかし、私はどんなクラスを取っていませんでした。 – hani

0

です。そのためには1つの配列をとる必要があります。あなたが名前の配列を呼び出すだけ取得

NSMutableArray *arraySelectObject; 

は、以下の選択またはラジオボタンを選択解除し、ボタンアクションをチェックすると、あなたの名前の配列が、その後5オブジェクト、

arraySelectObject=[[NSMutableArray alloc]init]; 
for (int i=0, i<=name.count,i++) 
{ 
    [arraySelectObject addObject:@"0"]; 
} 

を返しますlines.Suppose

-(IBAction)selectButton:(id)sender 
{ 
if([arraySelectObject objectAtIndex:sender.tag]==true) 
{ 
    [arraySelectObject replaceObjectAtIndex:sender.tag withObject:@"0"]; 
} 
else 
{ 
    [arraySelectObject replaceObjectAtIndex:sender.tag withObject:@"1"]; 
} 

[tableView reloadData]; 
} 

そして、あなたのすべてクリアボタンでメソッド

- (IBAction)clearAllBtnActn:(id)sender 
{ 
    for (int i=0, i<=name.count,i++) 
    { 
     [arraySelectObject addObject:@"0"]; 
    } 

    [tableView reloadData]; 
} 

とのtableView cellForrow方法で選択を表示し、ラジオボタンの画像または任意のコード

if([arraySelectObject objectAtIndex:sender.tag]==true) 
{ 
    //Set the image or your code of the select radio button ///// 

} 
else 
{ 
////////// Set the image or code of the Deselect radio button /////// 
} 
1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    CustomCell *cell = [tableview cellForRowAtIndexPath:indexPath]; 
    BOOL b = cell.check; 

    if(b) 
    { 
     cell.check = NO; 
     [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-uncheck.png"] forState:UIControlStateNormal]; 
    } 
    else{ 
     cell.check = YES; 

     [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-check.png"] forState:UIControlStateNormal]; 
    } 
} 
関連する問題