Home > Software design >  Separate array from core-data in tableView Sections
Separate array from core-data in tableView Sections

Time:12-23

I'm trying to sort array/names from my first TableViewController using core data to pass the data to my second UITableViewController, I have successfully been able to implement the number of sections and the correct number of rows for each section that the user needs by dividing the number of players with the number of sections.

However, I've been having problems separating the array of players continuously in each section. Cannot add the image of what the simulator looks like so I'll try to explain.

User types 10 names inside the first tableView, then selects the number of teams using a stepper(2 teams), lastly, they click on the team-up UIButton which divides 10(# players) by 2(number of teams) so on the second tableView two Sections will appear with 5 players but both Sections repeat the first 5 names.

I would like for the first Section to display the first 5 names of the array and the second Section to display the last 5 names instead of repeating the first 5 names on every Section regardless of how many players per Section the user chooses. I've been stuck for 4 days and have tried loops and Extensions and I cannot find a way for the rest of the sections to tap in the middle of the array of names, Please Help!! and thank you.

Here is the code of my secondTableView where I think the problems are either inside my tableView cellForRowAt or my loadedShuffledPlayers() function,

Note: Players comes from core data

import UIKit
import CoreData

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    loadedShuffledPlayers()
    tableView.reloadData()
    
}

var players2 = [Players]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var numberOfTeams = Int()

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return players2.count / numberOfTeams

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "shuffledNames", for: indexPath)
    cell.textLabel?.text = players2[indexPath.row].names
    return cell
}

func loadedShuffledPlayers(){
    
    let request: NSFetchRequest<Players> = Players.fetchRequest()
    do{
        players2 = try context.fetch(request).shuffled()
    }catch{
        print("Error fetching data .\(error)")
    }
    
}

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

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Team # \(section   1)"
}

}

CodePudding user response:

The problem is here

cell.textLabel?.text = players2[indexPath.row].names

You are only looking at the row number and are ignoring the section number. So with your example of 10 and 2 the row will always be between 0 and 4.

So you need to do something like (not tested):

let rowsPerSection = players2.count / numberOfTeams
let rowInSection = indexPath.row   rowsPerSection * indexPath.section

cell.textLabel?.text = players2[rowInSection].names
  • Related