-1

I get it to load, I get the sections to load, but I cant get the cells in each section to load correctly. It restarts from the beginning in each section effectively duplicating each cell. My entity name is Customer and it holds the following attributes. firstName, lastName, phoneNumber,email, notes, and first. First is the first letter in the last name to use for the sections. I can get it to load, the headers load successfully but as soon as I start adding names with the same first letter in the last name, it starts messing up. Let me know if you need any more code.

override func tableView(tableView: UITableView,
        cellForRowAtIndexPath
        indexPath: NSIndexPath?) -> UITableViewCell
{

        //getSections()
        let cell =
        tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!)

        let cust = customers[(indexPath?.section)!]
        print(indexPath?.row)
        let fName = cust.valueForKey("firstName") as? String
        print(fName)
        let lName = cust.valueForKey("lastName") as? String
        print(lName)
        cell.textLabel!.text = fName! + " " + lName!
        print("Cell: \(cell.textLabel!.text)")

        return cell


}
override func viewDidLoad() {
        super.viewDidLoad()


    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext
    context = managedContext

    //2
    let fetchRequest = NSFetchRequest(entityName:"Customer")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastName", ascending: true)]

    //3
    //var error: NSError?

    let fetchedResults = try!
        managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]

    if var results = fetchedResults
    {
        customers = results

    }
    else
    {
        print("Could not fetch")
    }

    tableView.reloadData()

    getSections()


}

    func getSections()
    {

    var sections:[String] = []
    for(var i=0;i<customers.count;i++)
    {
        let cust = customers[i]
        sections.append(cust.valueForKey("first") as! String)
    }
    sectionHeadersTotal = sections
    let unique = Array(Set(sections))
    sectionHeaders = unique.sort()
    print(sectionHeaders.count)
    //sectionHeaders = unique

    //return unique.count
}
Johnny Cox
  • 1,873
  • 3
  • 17
  • 17
  • what value passed in `numberOfSectionsInTableView` and `numberOfRowsInSection` Method? – iDhaval Jun 15 '15 at 06:16
  • The numberOfSectionsInTableView and numberOfRowsInSection all work correctly. If I add 3 customers with the last names starting with c, c, and z. It has 2 cells created for c and one for z. If I add one customer for each last name character it works perfectly. But when I add another customer with the same first character in their last name, then every section starts with whichever customer is first in the customers array. – Johnny Cox Jun 15 '15 at 06:38
  • When you say it starts messing up can you let us know what's happening whats the console output – Qazi Jun 15 '15 at 06:55
  • There is nothing in the console output. It runs fine, it just doesnt display the cells properly. If I add 4 customers with the last names Reed, Ramirez, Smith, and April it will have 3 sections with the headers of A, R, and S. A will have April, R will have April and Ramirez, and S will have April. And if I added a customer named Zeller, it would add another section for Z but would have April in there as well. – Johnny Cox Jun 15 '15 at 06:59
  • can you write your `getSections()` method?? – iDhaval Jun 15 '15 at 07:01
  • getSections() is what grabs the first letter of each customer and puts them in an array. It also creates an array that has no duplicate names for the sections. I posted it above. – Johnny Cox Jun 15 '15 at 07:05
  • Your customers array looks like it will contain every customer in the table, you are not performing any sort of filtering? As you go through creating the sections, you could create an Array of Arrays that hold the relevant customers for that section – Flexicoder Jun 15 '15 at 07:21
  • That is what I am having trouble with. I have tried using NSFetchResults but I cant seem to get that to work. If I create an Array of Arrays that holds only certain customers than how do I pass the whole customer to the cell so that when it is pressed, the correct information passes to the detailViewController? – Johnny Cox Jun 15 '15 at 07:25
  • You seem to create a section for every unique first name instead of last name as evident from your getSections implementation? – Shripada Jun 15 '15 at 08:44
  • No "first" is a value that stores the first letter of the last name. The for loop goes through every customer and saves to sections "first", which is an attribute in the entity for the first letter of the last name. – Johnny Cox Jun 15 '15 at 09:15

1 Answers1

0

Take a look here for the approach to read section-wise data -

http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/ and here iOS UITableView sections with fetchedResultsController confusion

Community
  • 1
  • 1
Tushar
  • 3,022
  • 2
  • 26
  • 26
  • Sorry for the late response, I took a look and I think this will help. If I successfully answer my questions with provided links I will come back and accept answer. Thanks. – Johnny Cox Jun 18 '15 at 00:27
  • I finally got it to work. I had to change my code up quite a bit so I cant really post the code. But, the two links provided is what helped solve it. Thanks again. – Johnny Cox Jun 18 '15 at 03:44
  • glad to hear that it helped you ! – Tushar Jun 18 '15 at 14:18