2012-10-29 4 views
30

iPadアプリを構築するときに、UICollectionViewCellの周囲にどのように境界線を描くことができますか?UICollectionViewCellボーダー/シャドウ

詳細:UICollectionViewCellを拡張したProductCellクラスを実装しました。さて、私はいくつかの細かい詳細を割り当てたいと思います。しかし、this hereのようなものを使用しようとすると、Xcodeは受信機タイプ 'CALayer'が前方宣言であることを知らせます。

#import <QuartzCore/QuartzCore.h> 

答えて

71

ただ、もう少し実装のために:あなたは、フレームワークQuartzCoreが含まれており、あなたのクラスにヘッダをインポートする必要があり

7

your.m

#import <QuartzCore/QuartzCore.h> 

を確認してくださいあなたのクラスは、

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

ここにセルが設定されています。

(石英がインポートされた後にのみ利用可能)あなたは、その後cell.layer.backgroundを変更することができ

参照

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    MyCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"pressieCell" forIndexPath:indexPath]; 
    //other cell setup here 

    cell.layer.borderWidth=1.0f; 
    cell.layer.borderColor=[UIColor blueColor].CGColor; 

    return cell; 
} 
11

以下スウィフト

あなたの Collection View set up with the required methodsを持っていると仮定するとスウィフト3

用に更新

境界線を追加するコードをいくつか書くだけで済みます。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 
    cell.myLabel.text = self.items[indexPath.item] 
    cell.backgroundColor = UIColor.cyan 

    // add a border 
    cell.layer.borderColor = UIColor.black.cgColor 
    cell.layer.borderWidth = 1 
    cell.layer.cornerRadius = 8 // optional 

    return cell 
} 

ノート

  • あなたがすでにUIKitをインポートした場合スウィフトにQuartzCoreをインポートする必要はありません。
  • また、影を追加する場合は、this answerを参照してください。
関連する問題