3

我想在数组中添加选定的单元格,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 等,并且对于每个级别,您可以选择有限的座位,然后需要为不同级别选择的座位添加不同的阵列。

4

1 回答 1

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
}

编辑(来自讨论的 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]
于 2013-05-24T09:56:58.557 回答