pavithra
pavithra

Reputation: 57

how to remove the array values when table view cell is not selected in objective c?

This is my code

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *selectedRows=[_mytableview indexPathsForSelectedRows];
NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
NSMutableArray *valueofselectedcell = [[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {

    customcell *cell = (customcell *)[_mytableview cellForRowAtIndexPath:indexPath];
    NSString *test = cell.balance.text;
    [valueofselectedcell addObject:test];
    NSArray *array = [valueofselectedcell copy];
    NSNumber* sum = [array valueForKeyPath: @"@sum.self"];
    NSLog(@"the sum valuie is .... %@",sum);
    NSString *totalamount = [sum stringValue];
    _amountlabel.text=totalamount;
    NSIndexPath *path = [tableView indexPathForSelectedRow];
    NSLog(@"%@ the selected path value is",path);
}}

when the table cell values are selected the selected values are store in the valueselectedcell array it's working fine.Now how to remove the values in the array when i deselect the rows .

Upvotes: 0

Views: 79

Answers (2)

amit
amit

Reputation: 21

I have not enough reputation to comment, so i'm posting it as a answer. The answer from @Dipankar Das is totally correct, i'm just trying to answer the question from OP's comment.

if unselected cell value is same to the selected value of the cell it means it will remove the selected value too in the array how to validate that.

Update didSelectRowAtIndexPath & didDeselectRowAtIndexPath method like this-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *selectedRows=[_mytableview indexPathsForSelectedRows];
NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
NSMutableArray *indexofselectedcell = [[NSMutableArray alloc]init];
NSMutableArray *valueofselectedcell = [[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {

    [indexofselectedcell addObject:indexPath];

    customcell *cell = (customcell *)[_mytableview cellForRowAtIndexPath:indexPath];
    NSString *test = cell.balance.text;
    [valueofselectedcell addObject:test];
    NSArray *array = [valueofselectedcell copy];
    NSNumber* sum = [array valueForKeyPath: @"@sum.self"];
    NSLog(@"the sum valuie is .... %@",sum);
    NSString *totalamount = [sum stringValue];
    _amountlabel.text=totalamount;
    NSIndexPath *path = [tableView indexPathForSelectedRow];
    NSLog(@"%@ the selected path value is",path);
}}


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

    if ([indexofselectedcell containsObject: indexPath]) {
        NSInteger anIndex=[indexofselectedcell indexOfObject:indexPath];
        [indexofselectedcell removeObjectAtIndex:anIndex];
        [valueofselectedcell removeObjectAtIndex:anIndex];
    }
}

Upvotes: 2

Dipankar Das
Dipankar Das

Reputation: 700

Use Tableview's didDeselectRowAtIndexPath delegate method:

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    customcell *cell = (customcell *)[_mytableview cellForRowAtIndexPath:indexPath];
    NSString *test = cell.balance.text;
    if ([valueofselectedcell containsObject: test])
    {
        [valueofselectedcell removeObject:test];
    }
}

Upvotes: 1

Related Questions