1

I'm trying to have a Table view mixed with static and dynamic sections. My dynamic section use a Xib file/class.

The output should look like that Result of the working app

Section 1 and 3 are static and section 2 is dynamic and display contact's names (from the nib).

I based my code on the example of Shan Ye on the page UITableView Mix of Static and Dynamic Cells?

on the simulator, i have 3 contacts and on the iPhone, my array has 4 to display. I print a comment with the name in the cellForRowAt and it's printing all the 4 names.

I get the right result on the simulator and it crashes on the iPhone with the error

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

I'm using Swift 4, Xcode 9.3 on an iPhone X with IOS 11.3.

My code is

import UIKit
import Contacts

class EditEvenementTableViewController: UITableViewController {

    var événement: Evenement? = nil


    override func viewDidLoad() {
        super.viewDidLoad()

        addData() // fill an array for test

        let nib = UINib.init(nibName: "DefaultParticipantTableViewCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "DefaultParticipantTableViewCell")

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    } // numberOfSections

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 1 {
            return (self.événement?.defaultParticipants.count)!
        }
        return super.tableView(tableView, numberOfRowsInSection: section)
    } // numberOfRowsInSection

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 {
            let participant = (événement?.defaultParticipants[indexPath.row])!
            let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultParticipantTableViewCell") as! DefaultParticipantTableViewCell
            cell.nomCompletLabel.text = participant.nomComplet
            return cell
        }
        return super.tableView(tableView, cellForRowAt: indexPath)
    } // cellForRowAt

} // EditEvenementTableViewController

Only suggestion or comment ?

Thanks

Pat N.
  • 35
  • 5
  • Refer this answer: https://stackoverflow.com/a/49157374/419348 It's important to override `tableView(_:,indentationLevelForRowAt:)`, and `tableView(_:,heightForRowAt:)` methods. – AechoLiu Nov 25 '21 at 01:48

2 Answers2

1

You can try again by adding the missing part below:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 1 {
            return 44
        }
        return super.tableView(tableView, heightForRowAt: indexPath)
    }
Tom B.
  • 124
  • 1
  • 11
0

First of all try to get rid of "!" in your code and try smth like this:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 1 {
        return self.événement?.defaultParticipants.count ?? 0
    }
    return 0
} // numberOfRowsInSection

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let événement = self.événement else { return super.tableView(tableView, cellForRowAt: indexPath) }
    if indexPath.section == 1 {
        let participant = événement.defaultParticipants[indexPath.row]            
        if let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultParticipantTableViewCell") as? DefaultParticipantTableViewCell {
        cell.nomCompletLabel.text = participant.nomComplet
        return cell
        }

    return UITableViewCell()
} // cellForRowAt
n_vart
  • 1
  • 2
  • 1
    Thanks, n_vart. I implemented the suggestions about the "!" and "if ... count > ... row". I have the same error message "index 3 beyond bounds [0 .. 2]' " – Pat N. Apr 23 '18 at 13:42
  • Sounds strange, i've edited code, try it. If it doen't work would you share your full source code? – n_vart Apr 23 '18 at 15:35
  • I recreated another project with just a simple example with no specific class. you can get the projet at the following link [https://www.icloud.com/iclouddrive/0zTngicw4PkFKHwfy-tIeXqVw#testStaticDynamicCellTV] (testStaticDynamicCellTV.zip) – Pat N. Apr 23 '18 at 19:57
  • in interfaceBuilder change tableview content to "Dynamic Prototypes" and run again. – n_vart Apr 24 '18 at 08:39
  • I already tried that to test the problem. I don’t want dynamic. It goes against what I want to do. If I put dynamic, i can’t design the static cells. – Pat N. Apr 24 '18 at 12:17
  • Pat N, your testStaticDynamicCellTV.zip is crashed on simulator. Why did you say NOT? – coders Feb 22 '19 at 19:44