Indexing UITableView

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Indexing UITableView

    Hallo liebe Profis,

    ich möchte gern in einem TableView Indexe haben, hab mir dazu im Inet auch was gesucht und scheitere nun leider an der override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell ich muß dort noch den code let contact = sections[indexPath.section][indexPath.row] einflechten aber bekomme irgendwie kein Ergebnis.
    Bitte um Hilfe oder besser Erklärung dazu.

    so sieht der bisherige Code aus:

    C-Quellcode

    1. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    2. let contact = sections[indexPath.section][indexPath.row]
    3. let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    4. let task = fetchedResultController.objectAtIndexPath(indexPath) as! Auftrag
    5. cell.textLabel?.text = "\(task.nachname!), \(task.vorname!)"
    6. cell.detailTextLabel?.text = "\(task.plz!) \(task.stadt!), \(task.strasse!)"
    7. return cell
    8. }
  • ja hab ich, nur die ganzen tutorials sind alle ohne diese coredata abfrage, deshalb tu ich mich da bestimmt etwas schwer.

    C-Quellcode

    1. import UIKit
    2. import CoreData
    3. class AuftragController: UITableViewController, NSFetchedResultsControllerDelegate {
    4. let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    5. var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
    6. let collation = UILocalizedIndexedCollation.currentCollation()
    7. var sections: [[AnyObject]] = []
    8. var objects: [AnyObject] = [] {
    9. didSet {
    10. let selector: Selector = Selector("localizedTitle")
    11. sections = Array(count: collation.sectionTitles.count, repeatedValue: [])
    12. let sortedObjects = collation.sortedArrayFromArray(objects, collationStringSelector: selector)
    13. for object in sortedObjects {
    14. let sectionNumber = collation.sectionForObject(object, collationStringSelector: selector)
    15. sections[sectionNumber].append(object)
    16. }
    17. self.tableView.reloadData()
    18. }
    19. }
    20. override func viewDidLoad() {
    21. super.viewDidLoad()
    22. fetchedResultController = getFetchedResultController()
    23. fetchedResultController.delegate = self
    24. do {
    25. try fetchedResultController.performFetch()
    26. } catch _ {
    27. }
    28. }
    29. // MARK:- PrepareForSegue
    30. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    31. if segue.identifier == "edit" {
    32. let cell = sender as! UITableViewCell
    33. let indexPath = tableView.indexPathForCell(cell)
    34. let taskController:StoreCustomerData = segue.destinationViewController as! StoreCustomerData
    35. let task:Auftrag = fetchedResultController.objectAtIndexPath(indexPath!) as! Auftrag
    36. taskController.auftrag = task
    37. }
    38. }
    39. // MARK:- Retrieve Tasks
    40. func getFetchedResultController() -> NSFetchedResultsController {
    41. fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
    42. return fetchedResultController
    43. }
    44. func taskFetchRequest() -> NSFetchRequest {
    45. let fetchRequest = NSFetchRequest(entityName: "Auftrag")
    46. let sortDescriptor = NSSortDescriptor(key: "nachname", ascending: true)
    47. fetchRequest.sortDescriptors = [sortDescriptor]
    48. return fetchRequest
    49. }
    50. override func didReceiveMemoryWarning() {
    51. super.didReceiveMemoryWarning()
    52. // Dispose of any resources that can be recreated.
    53. }
    54. // MARK: - TableView data source
    55. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    56. let numberOfSections = fetchedResultController.sections?.count
    57. return numberOfSections!
    58. }
    59. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    60. let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects
    61. return numberOfRowsInSection!
    62. }
    63. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    64. //let contact = sections[indexPath.section][indexPath.row]
    65. let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    66. let task = fetchedResultController.objectAtIndexPath(indexPath) as! Auftrag
    67. cell.textLabel?.text = "\(task.nachname!), \(task.vorname!)"
    68. cell.detailTextLabel?.text = "\(task.plz!) \(task.stadt!), \(task.strasse!)"
    69. return cell
    70. }
    71. // MARK: - TableView Delete
    72. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    73. let managedObject:NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as! NSManagedObject
    74. managedObjectContext.deleteObject(managedObject)
    75. do {
    76. try managedObjectContext.save()
    77. } catch _ {
    78. }
    79. }
    80. override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
    81. return "Löschen"
    82. }
    83. // MARK: - TableView Refresh
    84. func controllerDidChangeContent(controller: NSFetchedResultsController) {
    85. tableView.reloadData()
    86. }
    87. // MARK: - TableView Section
    88. override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
    89. return collation.sectionTitles[section]
    90. }
    91. override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String] {
    92. return collation.sectionIndexTitles
    93. }
    94. override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
    95. return collation.sectionForSectionIndexTitleAtIndex(index)
    96. }
    97. }
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von matze511 ()

  • Erledigt!!!

    hab mir das ganze nochmal richtig angeschaut und festgestellt, dass mein Code vorher ja schon eine Sortierung beinhaltet hatte und nur die IndexHeader fehlten.......oh man ?(
    nun noch im sectionNameKeyPath: "initialNom" kein nil und in der NSManagedObject Class noch etwas und siehe da es läuft.

    Danke trotzdem an @macmoonshine für die Hilfestellung, die kleinen Brocken regen zum Nachdenken an ;)