2016-08-24 6 views
0

私は1つのオブジェクトの異なる情報を表示するためにunclickable tableViewを使用しています。 この情報では、マップに配置した場所、オブジェクトに場所がある場合、リンクがあるリストがある場合、小さな説明の場合は複数の行ラベルなど、異なるカスタムセルタイプがあります。これまでのところは良い複数のカスタムセルの変数の使用

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

if indexPath.row == 0 { 
    let cell: mapCell = tableView.dequeueReusableCellWithIdentifier("mapCell") as! MapCell 
    return cell 
} else if indexPath.row == 1 { 
    let cell: textCell = tableView.dequeueReusableCellWithIdentifier("textCell") as! TextCell 
    return cell 
} else if indexPath.row == 2 { 
    let cell: listCell = tableView.dequeueReusableCellWithIdentifier("listCell") as! ListCell 
    return cell 
} 

} 

、すべての作業罰金:

は、私がこのセルを管理します。私の問題は、すべてのオブジェクトが地図を必要としているわけではなく、いくつかはテキストとリストだけが必要であり、他のオブジェクトはマップやリスト、その他すべてが必要です。私は条件がある場合、私のtableViewいくつかのセルをスキップしたい。

私は、私のtableViewのセルの数を変更するためのシンボリックな配列を作ることができますが、私は特定のセルではなく、私のtableViewの最後から削除することができます。

私はこのような何かを行うことができるように私のアイデアの一つは、多分、0または1の高さで、空のセルを生成することです:がない場合、私は知らない

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

if indexPath.row == 0 { 
    if mapCellNeeded { 
      let cell: mapCell = tableView.dequeueReusableCellWithIdentifier("mapCell") as! mapCell 
    } else { 
      let cell: emptyCell = tableView.dequeueReusableCellWithIdentifier("emptyCell") as! EmptyCell 
    } 
    return cell 
} else if indexPath.row == 1 { 
    ... 
}... 
} 

プット効率的な方法です。あなたたちが私を助けてくれることを願っています。

+0

必要なセル情報を保持するオブジェクトの配列を作成し、空のセルをその配列内にあるものと同じ数だけ作成しないでください。空のセルを作って、それがまったく必要ないことを意味するならば、 –

答えて

0

あなたのソリューションは機能します。

enum InfoCellType { 
case Map 
case Text 
case Links 
} 

... 

var rows = [InfoCellType]() 
... 
// when you know what should be there or not 
func constructRows() { 

if (mapCellNeeded) { 
rows.append(InfoCellType.Map) 
} 
rows.append(InfoCellType.Text) 
... etc 
} 

を次にテーブルビュー方法でちょうど現在のindexPath用タイプ何を参照してください:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

let cellType: InfoCellType = self.rows[indexPath.row] 
switch cellType { 
case .Map: 
    let cell: mapCell = tableView.dequeueReusableCellWithIdentifier("mapCell") as! mapCell 
    return cell 
case .Text: 
    ... 
case.Links: 
    ... 
} 
} 
別のアプローチは、(非常に素晴らしく、swifty)行番号をハードコーディングではなく、代わりに列挙型を使用するのではないだろう

この解決策では、行の順序を簡単に変更することもできます。rows配列内の項目の順序を変更するだけです。

+1

うん、それはとても素敵に見える、私はそのトモロウとレポートを試みる。ありがとう – kuemme01

+1

明日まで待つことができませんでした。それは完全に動作します!私はこのフォーラムが大好き!グリーティング – kuemme01

+0

私は助けることができる嬉しい:) – hybridcattt

関連する問題