【问题标题】:Using NSArray to populate collectionView使用 NSArray 填充 collectionView
【发布时间】:2017-07-28 06:32:16
【问题描述】:

我是 Objective-C 的新手,我有一个接受 NSArray 作为参数的函数,现在我想将该数组存储在我的函数中的本地 var 中,并且然后通过该数组填充集合视图,ps 我还必须更新同一类中的cellForItemAtIndexPath 委托方法。

这是tableView的Cell类中的方法。这里我在单元格中嵌入了一个collectionView。

-(void)initCellWithSetsArray:(NSArray *)setsArray{

NSMutableArray *setArray = [NSMutableArray array];    
}

这是我调用这个方法的地方,它接受 cellForRowAt tableView 的委托方法中的数组。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        [cell initCellWithSetsArray :self.setsHistoryArray];
}

所以当在cellForRowAtIndexPath 中调用时,我需要根据在func 中解析的数组来更新collectionView。我希望你们现在得到照片。

【问题讨论】:

  • 您有任何可以显示的代码吗?有许多关于集合视图的教程。您不能将数组存储在局部变量中;您需要将其存储在视图控制器的属性中,因为它需要可供cellForItemAt
  • @Paulw11 实际上这个函数在 TableViewCells 的类中,并且一个集合视图嵌入在单元格的 nib 文件中。所以我在 tableView 的委托方法的 cellforRowAtIndexPath 中调用了这个函数,我还需要用我在单元类的函数中解析的相同数组更新 collectionview
  • @Nirmalsinh 我已经用一些代码编辑了这个问题。如果可以,请提供帮助。
  • 根据您使用的是 UICollectionView 还是 UITableView,会调用不同的方法。除非有很好的理由这样做,否则我会避免在单个视图中使用两个 UI 元素。如果有疑问,请使用 UICollectionView(它有更多功能)。下面的方法应该可以工作,但一定要正确初始化你的 UICollectionView,设置委托和数据源。如果您不确定,请发布您的代码。

标签: ios objective-c uicollectionview nsarray


【解决方案1】:

您需要在单元类中添加一个属性来保存数组:

@property (strong, nonatomic) NSArray *setsArray;

-(void) setSetsArray(NSArray *)sets {
     _setsArray = sets;
     [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView 
 numberOfItemsInSection:(NSInteger)section {
    return [self.setsArray count];
}

您可以在单元类的cellForItemAt 方法中使用此数组。

此外,在您的单元格类prepareForReuse 中,您应该清除现有数组并重新加载集合视图:

-(void)prepareForReuse {
    self.setsArray = nil;
    [self.collectionView reloadData];
}

然后您可以简单地在cellForRowAt: 中分配属性:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCellClass *cell = (MyCellClass *)[tableView dequeueReusableCellWithIdentifier:"MyCellIdentifier" forIndexPath:indexPath];
    cell.setsArray = self.setsHistoryArray;  // Note: This looks wrong; - you should normally use indexPath.row to get the right data for this row
    return cell;
}

【讨论】:

    【解决方案2】:

    试试这个;

    pragma mark CollectionView

    -(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        return 1;
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return arrNames.count;
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"areacell" forIndexPath:indexPath];
        
       
        UILabel *lblName = (UILabel *)[cell viewWithTag:101]; // set tag in storyboard for this label
        lblName.text = arrNames[indexPath.item];
        
        return cell;
    }
    

    【讨论】:

    • 您的收藏视图中有多个部分吗?
    • 单元格中有两个视图,例如视图 A 和视图 B,我在视图 A 中嵌入了 collectionView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多