在通用应用程序中使用UICollectionView

avr*_*rit 5 objective-c uicollectionview uicollectionviewcell ios7

我需要创建一个带有列表的通用应用程序.点击一个单元格,它会显示单元格的详细视图.我通过在UIViewController中使用UICollectionView为iPad创建了列表.但是,当我在iPhone中尝试相同时,它无法正常显示.它是iPad细胞的缩放版本.

对于iPad,我需要像http://i.stack.imgur.com/8mJnI.png这样的单元格

对于iPhone,我需要像http://i.stack.imgur.com/moUxm.png这样的单元格

做这个的最好方式是什么?

任何帮助将不胜感激

Gre*_*reg 11

我建议使用像image,title,desc和抽象类的两个子类(例如iPadCell和iPhoneCell)这样的属性来创建抽象UICollectionViewCell的子类.

在故事板中添加两个原型单元并将其类和标识符更改为iPhoneCell和iPadCell.根据需要布置单元格并将collectionView:cellForItemAtIndexPath:右单元格出列,以获取适当的设备:

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

    YourAbstractClass *cell = nil;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
        // Make sure it match storyboard identifier for iPad cell
        cell = [cv dequeueReusableCellWithReuseIdentifier:@"iPadCellIdentifier" forIndexPath:indexPath];
    }
    else { //iPhone device
        cell = [cv dequeueReusableCellWithReuseIdentifier:@"iPhoneCellIdentifier" forIndexPath:indexPath];
    }
    cell.imageView.image = ...;
    cell.title = ...;
    cell.description = ...;

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

以非常类似的方式设置单元格大小,如果它与iPhone/ipad不同:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
            return CGSizeMake(400.0f, 500.0f);
        }
        return CGSizeMake(200.0f, 300.0f)

}
Run Code Online (Sandbox Code Playgroud)