私は独自のヘッダーを持つ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
}
}
私はあなたのデータモデルを変更すべきだと思います。セクション内の行の数は、各タスクの 'importanceLevel'パラメータによって決定する必要があります。 'cellForRowAt'メソッドはセクション内の各行に対して適切なセルを表示するだけです。データをフィルタリングする前に行う必要があります –
http://blog.apoorvmote.com/uitableview-with-multiple-sections-ios-swift/これは基本的なものです... – Spynet
サンプルはありますか?ありがとう –