2016-09-20 3 views
0

ここではBOOlの値を使用しています。TableViewの各セクションの値です。最初のセクションには静的なデータがあり、2番目のセクションには動的なデータがあります。セクションの各行にブール値が必要です。しかし、私はそうすることができません。第2セクションには動的なデータがあるからです。私はどのように静的か動的かの各セルをブール値として設定することができますか?すべてのTableViewCellにBOOL値を設定します

ここに私のコードです。どのように私はこれを実装することができ、上記のコードで

BOOL zero,one,dynamic 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: 
(NSInteger)section{ 
    if (section == 0) { 
     return 2; 
    }if (section == 1) { 
     return [myData count]; 
    } 
    return nil; 
} 



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{ 

if (indexPath.row == 0 && indexPath.section == 0) { 

     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.accessoryView.hidden = NO; 
     zero = true; 


    }if (indexPath.row == 1 && indexPath.section == 0) { 
     one = true; 

     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.accessoryView.hidden = NO; 

    } 

if (indexPath.section == 2) { 

     cell = [tableView cellForRowAtIndexPath:indexPath]; 
dynamic = true; 
} 
} 

は、私はあなたが私を提案してくださいすることができ、セクション2に BOOL値各行をしたいですか?

ありがとうございました。

+0

セル内の状態を反映するブール値プロパティをモデルに作成します。モデルの値を変更し、テーブルビューをリロードします。基本的にはビューを操作しないでください。 – vadian

答えて

1

あなたがそうちょうど0

例として、あなたのモデルに1 複数のキーと設定されたデフォルト値を追加するごとに動的なデータをBOOL値を追加する場合のような単純なトリックがあります: -

 NSArray *responseArray = responseObject; //WEB SERVICE RESPONSE 

// Add web service data to model 

    [responseArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 
     dataModel *data = [dataModel modelObjectWithDictionary:obj]; 
     [email protected](0) 
     [mydata addObject:objBillBorad]; 
    }]; 

または別の方法も

がありますされますが、簡単なNSDictionaryのの@を追加することができますように:あなたはmydata

へのWebサービスからデータを追加するたびに、{「isBoolValue」@(0)@}

これはあなたの問題を解決するかもしれないと思います。

問題がある場合は教えてください。私はできるだけ早く解決します。

+0

@ raviありがとう、コードのいくつかを表示してくださいできますか? –

+0

@ G.P.Reddy私はすでにサンプルコードを書いていますが、実際にあなたが探しているコードを教えていただけますか?それで試してみます。 –

0

これはtable viewの完全実装で、.mです。 NSMutableArrayを作成し、ユーザーがセルをタップしたときにデータを追加します。ユーザーがセルをタップすると、section,rowboolの値が表示されます。これを見てください。これがあなたに役立つことを願っています。

@interface ViewController() 

@end 

@implementation ViewController 
{ 
    NSMutableArray *holdingArray; 
    Boolean _zero,_first,_dynamic; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    holdingArray = [[NSMutableArray alloc] init]; 
    _zero = _first = _dynamic = FALSE; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 0) 
    { 
     return 2; 
    } 
    else 
    { 
     return 10; // you can use you dynamic data here. 
    } 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     return @"SECTION 0"; 
    } 
    else 
    { 
     return @"SECTION 1"; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *thecell = [tableView dequeueReusableCellWithIdentifier:@"mycell"]; 

    if(indexPath.section == 0) 
    { 
     //do your static data binding here 
    } 
    else 
    { 
     // do your dynamic data binding here 
    } 

    return thecell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     //this means your static data section 

     if(indexPath.row == 0) 
     { 
      //this is your zeroth row in zeroth section 
      NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section]; 
      NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; 
      _zero = TRUE; 

      NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | Zero Value : [%@]",mysection,myrow,@"TRUE"]; 

      [holdingArray addObject:addstring]; 

     } 
     else 
     { 
      //this is your first row in zeroth section 
      NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section]; 
      NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; 
      _first = TRUE; 

      NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | First Value : [%@]",mysection,myrow,@"TRUE"]; 

      [holdingArray addObject:addstring]; 
     } 


    } 
    else 
    { 
     //this means your dynamic data section. 

     NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section]; 
     NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; 
     _dynamic = TRUE; 

     NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | Dynamic Value : [%@]",mysection,myrow,@"TRUE"]; 

     [holdingArray addObject:addstring]; 
    } 

    NSLog(@"selected cells details : %@", holdingArray); 
} 




@end 
関連する問題