[Solved] Swift: fatal error: Index out of range


Create two sections One for HomeArr and one for AutoArr. I believe for each section you wanna show a additional cell with some title. So below code should help you.

extension ViewController : UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return (homeArr.count > 0) ?  homeArr.count + 1 : 0
        }
        else {
            return (autoArr.count > 0) ?  autoArr.count + 1 : 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "serviceCell",for:indexPath) as! servicesCell
        if indexPath.section == 0 {
            if (indexPath.row == 0)
            {
                cell.serviceLabel!.text = "Home"
                cell.contentView.backgroundColor = UIColor.blue
            } else {
                let home_dict = self.homeArr[indexPath.row - 1]
                cell.serviceLabel!.text = home_dict.service_name
                cell.serviceIcon!.image = UIImage(named:"\(home_dict.service_icon)")
            }
        }
        else {
            if (indexPath.row == 0)
            {
                cell.serviceLabel!.text = "Personal"
                cell.contentView.backgroundColor = UIColor.blue
            } else {
                let auto_dict = self.autoArr[indexPath.row - 1]
                cell.serviceLabel!.text = auto_dict.service_name
                cell.serviceIcon!.image = UIImage(named:"\(auto_dict.service_icon)")
            }
        }
        return cell
    }
}

EDIT:

As pointed out by rmaddy in comments below

Why the extra row in each section? Why not use a section header
instead?

As we are not aware of OP’s exact requirement I am updating my code to show section title as well.

 extension ViewController : UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return homeArr.count
        }
        else {
            return autoArr.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "serviceCell",for:indexPath) as! servicesCell
        if indexPath.section == 0 {
            let home_dict = self.homeArr[indexPath.row]
            cell.serviceLabel!.text = home_dict.service_name
            cell.serviceIcon!.image = UIImage(named:"\(home_dict.service_icon)")
        }
        else {
            let auto_dict = self.autoArr[indexPath.row]
            cell.serviceLabel!.text = auto_dict.service_name
            cell.serviceIcon!.image = UIImage(named:"\(auto_dict.service_icon)")
        }
        return cell
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "Home Arr cell"
        }
        else {
            return "Auto Arr cell"
        }
    }
}

16

solved Swift: fatal error: Index out of range