2016-07-06 6 views
0

デフォルトのiOSカレンダーアプリケーションとよく似たデモアプリケーションを作成しようとしています。私はほとんど終わっていますが、ベストプラクティスがTableViewにカスタムセルを適用しているかどうかは完全にはわかりません。TableView CellのプログラムまたはXIB実装

デフォルトのアプリは、スクリーンショット(下記参照)を見て、特定の日のすべてのイベントのTableViewを持っています。そのスタイリングを自分のセルに適用するにはどうすればいいですか?現時点で

、私はこれを持っている(基本的に私のviewDidAppear方法で呼び出さ:ストーリーボードで定義されているラベルとして

  • 月のタイトル
  • 私は
  • デフォルトプログラムで適用しています
  • カレンダーパッケージiOS Tableviewもこのプログラムの下にプログラムで適用されます

TableViewはプログラムによって適用されるため、どこに行くのかが完全にわかりません。 ELOWは、テーブルビューのために私のコードです:

let cellReuseIdentifier = "eventCell" 
let offsetY = {% Some fancy calculation here to get offset pos %} 
let height = {% Just as above but to determine remaining height %} 

tableView.frame = CGRectMake(0, offsetY, screenSize.width, height) 
tableView.delegate = self 
tableView.dataSource = self 
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) 

view.addSubview(tableView) 

特定のデータソースのメソッド:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return selectedEvents.count 
    } 

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

     // create a new cell if needed or reuse an old one 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier)! 
     let event = selectedEvents[indexPath.row] 

     // set the text from the data model 
     cell.textLabel?.text = "\(event.startTime) - \(event.endTime) | \(event.summary)" 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print("You tapped cell number \(indexPath.row).") 
    } 

どれ指導をいただければ幸いです:)

iOS Calendar

答えて

0

それはのようなものになるはずですこれは:

tableView.registerClass(CustomCellClass.self, forCellReuseIdentifier: "CustomCellClasId") 

そして:この回答@sukh

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellClassId", forIndexPath: indexPath) as? CustomCellClass{ 
      // set up cell 
      return cell 
     } 
} 
+0

は、カスタムセルクラスを使用する方法である - それはプログラムで実装またはXIBはあなた次第ですが、私の経験ではほとんどの人が設計するxibsが簡単に見つけていますか。 – bradkratky

+0

ありがとう@ andrew-mckinleyと@bradkratky - これを動作させるために私は 'registerClass'行を削除しなければなりませんでした - これは正しいのですか?必要に応じて詳細を追加することができます。基本的には、対応するXIBファイルとともに新しいサブクラス化された 'UITableViewCell'を作成しました。再利用識別子を変更し、 'cellForRowAtIndexPath'メソッドを修正して、セルがnilであるかどうかを調べました。それが無ければ、私は次の行を使用します: 'tableView.registerNib(UINib(nibName:" RoomDetailTableViewCell "、バンドル:nil)、forCellReuseIdentifier:" roomDetailCell ")'を呼び出して、基本的にセルをデキューします – sukh

+1

愚かな - 私は別のStackOverflowの投稿をここで見つけたhttp://stackoverflow.com/questions/28999400/swift-uitableview-custom-cell-programatically-documentation - これに従うだけ:) – sukh

関連する問題