在不同的数组中添加不同部分的选定单元格,UICollectionView

我想在数组中添加UICollectionView选定单元格,不同数组中的部分UICollectionView表示不同的数组。 问题是这个部分是dynamic的。 以下是我的代码。

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

数组arrSeatSel获得可以从每个部分中select的部分的数量和座位的数量。

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

这里的部分是2,可以select的单元格是2.对于其他部分和在所有情况下都是类似的。

arrTot正在获取每个部分的单元格总数

 description of arrTot( 10, 10 ) 

数组arrLevels是段的数量。 而数组itemPaths正在添加选中的单元格,这里的问题是无论哪一部分是添加选中的单元格,但是每一部分都有自己的select单元格的限制。 希望你明白我的观点,如果有的话可以自由地问清楚。 总之,我告诉你这里发生了什么是不同级别的座位图1,2级等,每个级别你可以select有限的座位,那么不同级别的select座位需要添加在不同的arrays。

使用字典来存储细节。 部分编号变成键并存储对应于每个键的选定项目的数组

这是纲要

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

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

编辑( 来自讨论的Imp代码

  // 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]