Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add selected cells of different sections in different arrays, UICollectionView

I want to add selected cells of UICollectionView in arrays, section wise in different arrays, means different arrays for each section. The problem is this that number of sections are dynamic. Below is my code.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *seatV;
    int cs;

    NSString *secVal = [arrSeatSel objectAtIndex:indexPath.section];
    NSArray *arrSplit = [secVal componentsSeparatedByString:@":"];
    seatV = [arrSplit objectAtIndex:1];
    cs = [seatV integerValue];

    int v;
    NSString *cnt = [NSString stringWithFormat:@"%@",[arrTot objectAtIndex:indexPath.section]];
    v = [cnt intValue];

    NSString *sect = [NSString stringWithFormat:@"%d", indexPath.section];

    if(indexPath.item < v)
    {
        if([sectionInfo count] < cs)
        {
            itemPaths = [self.collectionView indexPathsForSelectedItems];

            sectionInfo = [NSMutableArray arrayWithArray: [self.collectionView indexPathsForSelectedItems]];
            [selectedItemsInfo setObject:sectionInfo forKey:sect];
            cell=[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]];                    
        }

        else
        {                       
            [self.collectionView deselectItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section] animated:YES];

            [sectionInfo removeAllObjects];
        }

        [self.collectionView deselectItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section] animated:YES];
    }

    NSLog(@"section array:%@", sectionInfo);
    NSLog(@"section array1:%@", sectionInfo1);
    NSLog(@"selected seats dict:%@", selectedItemsInfo);
}

The array arrSeatSel is getting the number of sections and number of seats that can be choose from each section.

description of arr seatsel:(
 "Family:2",
 "Gold:3"
)

Here sections are 2 and cells that can be selected are 2. Similarly for other sections and in all cases.

arrTot is getting the total number of cells in each section

description of arrTot(
    10,
    10
)

array arrLevels are number of sections. and array itemPaths is adding the selected cells, and here the problem is, whichever section is it is adding selected cells, but each section has it's own limit of selecting cells. Hope you got my point, if anything clear ask freely. In short i tell you what is happening here is there is seat map for different levels level 1, 2, etc. and for each level you can select limited seats, then those selected seats for different levels are required to add in different arrays.

like image 447
iPhone Programmatically Avatar asked Jan 31 '26 20:01

iPhone Programmatically


1 Answers

Use a dictionary to store the details. section number becomes the keys and store an array of selected items corresponding to each keys

Here is outline

 NSDictionary 
     Key:section0  value: array of selected items in section0
     Key:section1  value: array of selected items in section1  

Code

 //Create a dictionary first 
 NSMutableDictionary *selectedItemsInfo = [NSMutableDictionary new];

// During selection
NSMutableArray *sectionInfo = [selectedItemsInfo objectForKey:indexPath.section];
if (sectionInfo == nil) {
     NSMutableArray *array = [NSMutableArray array]
    [array addObject: ] // add selected item
    [selectedItemsInfo setObject:array forKey:indexPath.section];

}
else
{
    [sectionInfo addObject: ]  // add selected item
}

Edit (Imp code from discussion)

 // Follow the below pattern
 NSMutableArray *sectionInfo = [selectedItemsInfo objectForKey: [NSNumber numberWithInt:indexPath.section]]; 

if (sectionInfo == nil) { 
     NSMutableArray *array = [NSMutableArray array]; 
     [array addObject: indexPath]; // add selected item 
     [selectedItemsInfo setObject:array forKey:[NSNumber numberWithInt:indexPath.section]]; 

  } 
  else 
  { 
      // check the count 
     if([sectionInfo count] < cs) 
     { 

      [sectionInfo addObject: indexPath]; // add selected item 
     } 
     else 
     { 
       // No need to add the item. Deselect the cell 
     } 
  }


  // To remove an item  
  sectionInfo = [selectedItemsInfo objectForKey: [NSNumber numberWithInt:indexPath.section]];
  [sectionInfo removeObject:indexPath]
like image 54
Anil Varghese Avatar answered Feb 03 '26 11:02

Anil Varghese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!