首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将不同部分的选定单元格添加到不同的数组中,UICollectionView

将不同部分的选定单元格添加到不同的数组中,UICollectionView
EN

Stack Overflow用户
提问于 2013-05-24 17:25:20
回答 1查看 1.5K关注 0票数 3

我想在数组中添加UICollectionView的选定单元格,在不同的数组中逐段添加,这意味着每个部分都有不同的数组。问题是,部分的数量是动态的。下面是我的代码。

代码语言:javascript
运行
复制
- (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获取可从每个区段中选择的区段数和座位数。

代码语言:javascript
运行
复制
description of arr seatsel:(
 "Family:2",
 "Gold:3"
)

这里的区段是2,可以选择的单元格是2。对于其他区段和所有情况也是如此。

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

代码语言:javascript
运行
复制
description of arrTot(
    10,
    10
)

数组arrLevels是节数。数组itemPaths正在添加选定的单元格,这里的问题是,无论哪个部分添加选定的单元格,但每个部分都有其自己的选择单元格的限制。希望你明白我的意思,如果有什么清楚的,请随意询问。简而言之,我在这里告诉你的是,这里有不同级别的座位图,级别1,2等,对于每个级别,你可以选择有限的座位,然后需要将不同级别的选定座位添加到不同的数组中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-24 17:56:59

使用字典来存储详细信息。段编号成为关键字,并存储与每个关键字对应的选定项的数组

下面是提纲

代码语言:javascript
运行
复制
 NSDictionary 
     Key:section0  value: array of selected items in section0
     Key:section1  value: array of selected items in section1  

代码

代码语言:javascript
运行
复制
 //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代码

代码语言:javascript
运行
复制
 // 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]
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16731644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档