2016-08-12 7 views
4

Objective Cの1つのxibの下に複数のuitableviewセルをロードすることは可能ですが、それも可能ですか? 複数のuitableviewセルを即座に1つのxibの下にロードする

は、私はObjective Cの

var cellAuditDetails:AuditDetailsTableViewCell! = tableView.dequeueReusableCellWithIdentifier("customTrialDetailsCell", forIndexPath: indexPath) as! AuditDetailsTableViewCell 

      if indexPath.row == 0{ 
       if(cellAuditDetails == nil) 
       { 
        cellAuditDetails = NSBundle.mainBundle().loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)[0] as! AuditDetailsTableViewCell; 
       } 
      } 
      else{ 
        cellAuditDetails = NSBundle.mainBundle().loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)[1] as! AuditDetailsTableViewCell; 
} 

enter image description here

に使用したのと同じロジックを使用してみました。しかし、私は1つのだけのセルの罰金を使用する場合はここで、次のエラー***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (customTrialDetailsCell) - nib must contain exactly one top level object which must be a UITableViewCell instance'

を得ました。しかし、同じxibの下に複数のセルを読み込むにはどうすればいいですか?その新しい細胞のために別のxibを取るために刺激するので。

+1

あなたはこれを試しましたか?http://stackoverflow.com/a/34504366/4687211? –

+1

なぜあなたは余分なペン先を使いたいのですか? Interface Builderのテーブルビューで、複数の異なるプロトタイプセルを簡単に作成できます。ところで、 'dequeueReusableCellWithIdentifier:forIndexPath:'は常に非選択的なセルを返すので、 'nil'になることはありません。 – vadian

+0

@Lu_:私は試みましたが、この機能はすぐには機能しません。 – Poles

答えて

0

あなたがあなたのXIBのビュー(タグ、カスタムクラスなど)を区別する場合は、あなたもペン先を登録する必要はありません可能です - あなたがしなければならないすべてはこれです:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cellIndex = 12345 
    var cellIdentifier = "MyAwesomeCell" 

    // dequeue cell or, if it doesn't exist yet, create a new one from the nib 
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)! 
    if !cell { 
     var topLevelObjects = Bundle.main.loadNibNamed("MyNibWithLotsOfCustomCells", owner: self, options: nil) 
     var index = (topLevelObjects as NSArray).indexOfObject(passingTest: {(_ obj: Any, _ idx: Int, _ stop: Bool) -> BOOL in 
       // if you distinguish the views by class 
       return type(of: obj) === NSClassFromString(cellIdentifier) 

       // if you distinguish the views by tag 
       return (obj as! UIView).tag == cellIndex 
      }) 
     cell = topLevelObjects[index] 
    } 
} 

dequeueReusableCell(withIdentifier:cellIdentifier) 

代わりの

dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 
+0

やってみます。 – Poles

+0

提案していただきありがとうございます。 – Poles

0

どういうわけか、私はSに管理し、その使用を覚えておくもう一つSwift 3でこの問題を解決してください。@ waris-shamsに感謝します。ここに解決策があります:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCell(withIdentifier: "customCell") 

    if indexPath.row == 0{ 
     if(cell == nil) 
     { 
      let cellAuditDetails:AuditDetailsTableViewCell = Bundle.main.loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)? [0] as! AuditDetailsTableViewCell 
      cell = cellAuditDetails 
     } 
     } 
     else{ 
      if(cell == nil) 
      { 
       let cellAuditDetails:AuditDetailsTableViewCell = Bundle.main.loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)? [1] as! AuditDetailsTableViewCell 
       cell = cellAuditDetails 
      } 
     } 
} 
関連する問題