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

时间:2013-05-24 09:25:20

标签: 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);
}

数组arrSeatSel获取可从每个部分中选择的部分数和席位数。

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

这里的部分是2,可以选择的单元格是2.类似的其他部分和所有情况。

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

description of arrTot(
    10,
    10
)

数组arrLevels是节数。并且数组itemPaths正在添加所选单元格,这里的问题是,无论添加所选单元格的哪个部分,但每个部分都有自己的选择单元格的限制。希望你明白我的观点,如果有任何明确的要求自由。 总之,我告诉你这里发生的事情是有不同等级1,2等的座位图,并且对于每个等级你可以选择有限的座位,那么不同等级的那些选定座位需要添加到不同的阵列。 / p>

1 个答案:

答案 0 :(得分:5)

使用字典存储详细信息。段号成为键并存储与每个键对应的所选项目数组

这是大纲

 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
}

编辑(讨论中的代码

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