根据UICollectionView

时间:2015-05-13 07:05:09

标签: ios objective-c uicollectionview

我在content size中有三种不同类型的单元格,其中imagelabelbuttonUICollectionview不同 CLS OPEN "com2:9600,n,8,1,BIN,cs,ds,rs" FOR RANDOM AS #1 param$ ="Some data" PRINT #1, param$ DO WHILE b$ <> "*CLOSE*" INPUT #1, b$ PRINT b$ LOOP 。我从Web服务获取数据。我想根据这些类型显示正确的单元格。

2 个答案:

答案 0 :(得分:1)

首先,为您的单元格注册布局的笔尖:

[collectionView registerNib:myCellTypeImageNib forCellWithReuseIdentifier:@"MyModelTypeImageCellIdentifier"];
[collectionView registerNib:myCellTypeLabelNib forCellWithReuseIdentifier:@"MyModelTypeLabelCellIdentifier"];
[collectionView registerNib:myCellTypeButtonNib forCellWithReuseIdentifier:@"MyModelTypeButtonCellIdentifier"];

然后适当地返回它们:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyModel *modelObject = self.dataArray[indexPath.item];
    UICollectionViewCell *cell = nil;
    switch (modelObject.type) {
        case MyModelTypeImage:
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyModelTypeImageCellIdentifier" forIndexPath:indexPath];
            //adjust cell
            break;
        case MyModelTypeLabel:
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyModelTypeLabelCellIdentifier" forIndexPath:indexPath];
            //adjust cell
            break;
        case MyModelTypeButton:
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyModelTypeButtonCellIdentifier" forIndexPath:indexPath];
            //adjust cell
            break;
    }
    return cell;
}

答案 1 :(得分:-1)

您可以使用

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

注意:indexpath

例如。将所有图片添加到array

self.myArray = [[NSArray alloc] initWithObjects:@"first.jpg",
                                 @"second.jpg",
                                 @"third.jpg",
                                 @"last.jpg",nil]

然后在cellForRow...中执行以下操作:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // configure cell
    ...
    cell.myImageView = [self.myArray objectAtIndex:indexPath.row;
}