iPh*_*lly 3 iphone ios uicollectionview
我想UICollectionView
在数组中添加选定的单元格,在不同的数组中分段,表示每个部分的不同数组.问题在于这些部分是动态的.以下是我的代码.
- (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);
}
Run Code Online (Sandbox Code Playgroud)
该阵列arrSeatSel
获得了每个部分可以选择的部分数量和座位数.
description of arr seatsel:(
"Family:2",
"Gold:3"
)
Run Code Online (Sandbox Code Playgroud)
这里的部分是2,可以选择的单元格是2.同样适用于其他部分和所有情况.
arrTot
获得每个部分中的单元格总数
description of arrTot(
10,
10
)
Run Code Online (Sandbox Code Playgroud)
数组arrLevels
是节数.并且数组itemPaths正在添加所选单元格,这里的问题是,无论添加所选单元格的哪个部分,但每个部分都有自己的选择单元格的限制.希望你明白我的观点,如果有任何明确的要求自由.总之,我告诉你这里发生的事情是有不同级别1,2等的座位图,并且对于每个级别,您可以选择有限的座位,然后需要将不同级别的所选座位添加到不同的阵列中.
使用字典存储详细信息.段号成为键并存储与每个键对应的所选项的数组
这是大纲
NSDictionary
Key:section0 value: array of selected items in section0
Key:section1 value: array of selected items in section1
Run Code Online (Sandbox Code Playgroud)
码
//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
}
Run Code Online (Sandbox Code Playgroud)
编辑(来自讨论的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]
Run Code Online (Sandbox Code Playgroud)