我想在数组中添加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);
}数组arrSeatSel获取可从每个区段中选择的区段数和座位数。
description of arr seatsel:(
"Family:2",
"Gold:3"
)这里的区段是2,可以选择的单元格是2。对于其他区段和所有情况也是如此。
arrTot正在获取每个部分中的单元格总数
description of arrTot(
10,
10
)数组arrLevels是节数。数组itemPaths正在添加选定的单元格,这里的问题是,无论哪个部分添加选定的单元格,但每个部分都有其自己的选择单元格的限制。希望你明白我的意思,如果有什么清楚的,请随意询问。简而言之,我在这里告诉你的是,这里有不同级别的座位图,级别1,2等,对于每个级别,你可以选择有限的座位,然后需要将不同级别的选定座位添加到不同的数组中。
发布于 2013-05-24 17:56:59
使用字典来存储详细信息。段编号成为关键字,并存储与每个关键字对应的选定项的数组
下面是提纲
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
}编辑(来自discussion)的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]https://stackoverflow.com/questions/16731644
复制相似问题