2016-12-21 16 views
0

私は独自のヘッダーを持つ3つのセクションを持つTableViewControllerを持っています。 これで、セルを挿入する前に、プロパティをチェックしてから、セルを別のセクションに追加します。UITableViewController異なるセクションに異なるセルを挿入します。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "TasksTableViewCell" 


    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell else { 
     fatalError("The dequeued cell is not an instance of TasksTableViewCell.") 
    } 

    // Fetches the appropriate task for the data source layout. 
    let task = tasks[indexPath.row] 

    cell.nameLabel.text = task.name 
    cell.photoImageView.image = task.photo 
    cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "") 

    if(task.importanceLevel == 0){ 
     // add cell to section 0 
    } 
    else if(task.importanceLevel == 1){ 
     // add cell to section 1 
    } 

    // Configure the cell... 

    return cell 
} 

コメントが表示されますか、それを行う方法はありますか? はあなたがモデルを作成し、データソースの方法で各セクションと行のために最初に空のカウント配列を渡すことができ、この

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    if(indexPath.section == 0){ 
     let cellIdentifier = "TasksTableViewCell" 

     guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell else { 
      fatalError("The dequeued cell is not an instance of TasksTableViewCell.") 
     } 

    // Fetches the appropriate task for the data source layout. 

     let task = tasks[indexPath.row] 
     cell.nameLabel.text = task.name 
     cell.photoImageView.image = task.photo 
     cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "") 

     return cell 
    } 
    else if(indexPath.section == 1) 
     let cellIdentifier = "TasksTableViewCell1" 

     guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell1 else { 
      fatalError("The dequeued cell is not an instance of TasksTableViewCell.") 
     } 

    // Fetches the appropriate task for the data source layout. 
     let task = tasks[indexPath.row] 
     cell.nameLabel.text = task.name 
     cell.photoImageView.image = task.photo 
     cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "") 

     return cell 
    } 
} 
+0

私はあなたのデータモデルを変更すべきだと思います。セクション内の行の数は、各タスクの 'importanceLevel'パラメータによって決定する必要があります。 'cellForRowAt'メソッドはセクション内の各行に対して適切なセルを表示するだけです。データをフィルタリングする前に行う必要があります –

+0

http://blog.apoorvmote.com/uitableview-with-multiple-sections-ios-swift/これは基本的なものです... – Spynet

+0

サンプルはありますか?ありがとう –

答えて

-2

、ありがとうございました。 そして、カウントを初期化して配列を塗りつぶし、次にテーブルビューをリロードします。 numberOfSectionsメソッドもあることをご存知でしょうか?

+0

urヘルプをありがとう。 .importanceLevelはlet task = tasks [indexPath.row]の後にしかアクセスできません.TasksTableViewCell1はありません。セルを追加するセクションが3つあります。 –

+0

@QuangDinhLuongコードを更新しました。現在アクティブなセクションに基づいてセルをロードする –

0

のようにそれを試すことができ、非常に

0

私はこのコードがお手伝いします思う:

let arr = [5,2,7,10,2] 
override func viewDidLoad() { 
    super.viewDidLoad() 
} 
// MARK: - Table view data source 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 5 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return arr[section] 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("celldata", forIndexPath: indexPath) 

    cell.textLabel?.text = "section \(indexPath.section)" 
    cell.detailTextLabel?.text = "Rows \(indexPath.row)" 

    return cell 
} 
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let view = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 20)) 
    let lbl = UILabel(frame: CGRect(x: 5, y: 5, width: self.tableView.frame.width, height: 15)) 
    lbl.text = "\(section)" 
    view.backgroundColor = UIColor.grayColor() 
    view.addSubview(lbl) 
    return view 
} 
関連する問題