2017-03-15 8 views
2

それぞれに 'color'文字列値を含む 'TagInstances'のリストがあります。私UICollectionViewでセルをスキップする方法/ GetCellメソッド、UICollectionViewからセルを返す方法

List<TagInstance> tags 

私はtagCellsの初期化を断念したい場合:

​​

イム "cellForItemAtIndexPath" でこれをやろうとしている/ "getcellを" 方法:

[Export("collectionView:cellForItemAtIndexPath:")] 
public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) 
{ 
    var cell = (TagCell)collectionView.DequeueReusableCell("tagCell", indexPath); 

    var thisTag = tags[indexPath.Row]; 

    if (thisTag.color == "undefined") { /* todo: Skip over this cell somehow */ } 

    try 
    { 
     var color = UIColor.Clear.FromHex(thisTag.color); 

     cell.SetUIWithData(color, thisTag.abbreviation); 

     cell.AddBorderAndShadow(); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine($"\ncolor : { thisTag.color }\nThrows an exception : \n{ ex }\n"); 
    } 

    return cell; 
} 

現在、私のコレクションビューは、TagInstanceがあった場所と定義されていない色の隙間を伴って表示されます。コレクションがそのギャップなしに現れるようにしたいと思います。これは可能ですか?

+3

に次

使用適切な解決策は、あなたのコレクションビューのデータソース内でこれらの値を含めないことです。 – rmaddy

+3

'undefined'要素を除外したフィルタリングされたデータセットを割り当てます。 – SushiHangover

答えて

3

rmaddyとSushiHangoverが言ったように、適切な方法は、表示したい項目だけを持つ正しいデータソースを持つことです。いくつかの方法で

は、代替だけでUICollectionViewでそうするために、幅/高さ= 0にこれらの不要な細胞のサイズを決定されるだろう、あなたは、あなたがいないそれらの要素のためのSize = CGSize.Emptyを返すためにUICollectionViewFlowLayoutから派生しLayoutAttributesForElementsInRectメソッドをオーバーライドする必要があります見たい。

サブクラス化UICollectionViewFlowLayoutXamarin Docs in Introduction to UICollectionViewにしてください。

また、SushiHangoverとrmaddyが提案しているように、データソースレベルで要素をフィルタリングするのが、より簡単で簡単な方法です。

1

1)データを削除したいデータソースからそのデータを削除し、コレクションビューにセルを表示します。

tags.RemoveAll(p => p.color== "undefined"); 

または

2)あなたはUICollectionViewDelegateFlowLayoutのGetSizeForItemメソッドを使用し、その他の理由でリストにそのデータを保持する場合。 UICollectionviewSource

[Export ("collectionView:layout:sizeForItemAtIndexPath:"), CompilerGenerated] 
public virtual CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath) 
{ 
var thisTag = tags[indexPath.Row]; 

if (thisTag.color == "undefined") 
     return new CGSize(0,0); 
else 
    return new CGSize (yoruwidth, yourheigh); 
} 
関連する問題