G.P.Reddy
G.P.Reddy

Reputation: 556

set a BOOL value in every TableViewCell

Here I am using BOOl Values in every section of TableView. First section have a static data and second section have a dynamic data.I want bool value in each row in the section. But I am unable to do so, because the second section have dynamic data. Can you please suggest me how can I set as bool value each cell either static or dynamic.

Here is my code.

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

In the above code I want BOOL values each row in the section 2,can you please suggest me how can I can I implement this ?

thank you.

Upvotes: 0

Views: 178

Answers (2)

caldera.sac
caldera.sac

Reputation: 5088

this is complete implementation for the table view in .m. create a NSMutableArray and add the data when user tap on a cell. this will show you which section, row and your bool value when user tap on a cell. have a look at this. hope this will help to you.

@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

Upvotes: 0

Ravi Panchal
Ravi Panchal

Reputation: 1355

There is simple trick like if you want to add BOOL value with Every Dynamic data so just add one more key to your model and set default value as 0.

Example:-

      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];
        data.isBOOLValue=@(0)
        [mydata addObject:objBillBorad];
    }];

or there is another way is there also

like you can add a simple NSDictionary @{@"isBoolValue":@(0)} every time when you are adding a data from web service to mydata

i think this may be solve your problem.

if it is not so please let me know if you have any issue so i will be resolve asap.

Upvotes: 1

Related Questions