在不同的数组UICollectionView中添加不同部分的选定单元格 [英] Add selected cells of different sections in different arrays, UICollectionView
问题描述
我想在数组中逐段添加 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);
}
数组 arrSeatSel
正在获取部分的数量和可以从每个部分中选择的座位数量.
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"
)
这里的部分是2,可以选择的单元格是2.对于其他部分,在所有情况下都是类似的.
Here sections are 2 and cells that can be selected are 2. Similarly for other sections and in all cases.
arrTot
正在获取每个部分中的单元格总数
arrTot
is getting the total number of cells in each section
description of arrTot(
10,
10
)
数组 arrLevels
是部分的数量.和数组itemPaths正在添加选定的单元格,这里的问题是,无论哪个部分都在添加选定的单元格,但是每个部分都有其自己的选择单元格限制.希望您明白我的意思,如果有什么清楚的问题,请随意提出.简而言之,我告诉您这里发生的是不同级别1、2等的座位图,并且对于每个级别,您都可以选择有限的座位,然后需要为不同级别选择的那些座位需要添加到不同的数组中./p>
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.
推荐答案
使用字典存储详细信息.部门号成为键,并存储与每个键相对应的所选项目的数组
Use a dictionary to store the details. section number becomes the keys and store an array of selected items corresponding to each keys
这是大纲
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代码)
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]
这篇关于在不同的数组UICollectionView中添加不同部分的选定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!