3lvis / DATASource

Core Data's NSFetchedResultsController wrapper for UITableView and UICollectionView
Other
106 stars 27 forks source link

Custom UITableViewCell #33

Closed syky27 closed 9 years ago

syky27 commented 9 years ago

Potentionaly really stupid question. Does this work with custom UITableViewCell? If so, can you please give me any hint. I cannot get it working. Thanks!

3lvis commented 9 years ago

Hi Tomas,

Not stupid at all. You just need to send the cell identifier of your custom registered cell.

Example:

Registered CustomCell, that's a subclass of UITableViewCell.

self.tableView.registerClass(CustomCell.self, forCellReuseIdentifier: CustomCell.Identifier)

Then you create your DATASource like this:

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: CustomCell.Identifier, fetchRequest: request, mainContext: self.dataStack!.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
    if let cell = cell as? CustomCell {
        cell.label.text = item.valueForKey("name") as? String
    }
})

self.tableView.dataSource = self.dataSource

Let me know if this works for you, otherwise feel free to reopen the issue :)

syky27 commented 9 years ago

That's exactly how I have it, so I double checked and I figured out that I had one more if above the assignation to cell, which was taking objects from database and casting them, and that's where it failed... (Just smashing my head agains wall, how stupid I am)

Thank you, sorry for wasting your time.

3lvis commented 9 years ago

@syky27 Don't worry, let me know if I you have any troubles understanding how it works. Documentation can always be improved :heart:

swabzz commented 7 years ago

Hello, I registered CustomCell, that's a subclass of UITableViewCell. But when assigning the value to label of subclass its giving me fatal error for nil variables as fatal error: unexpectedly found nil while unwrapping an Optional value

here is my code..

{ self.dataStack = DATAStack(modelName: "modelname") self.tableView.register(NotificationsTableViewCell.self, forCellReuseIdentifier: "NotificationsTableViewCellIdentifier") self.tableView.dataSource = self.dataSource }

lazy var dataSource: DATASource = { let request: NSFetchRequest = NSFetchRequest(entityName: "Requests") request.sortDescriptors = [NSSortDescriptor(key: "request_id", ascending: true)]

    let dataSource = DATASource(tableView: self.tableView, cellIdentifier: "NotificationsTableViewCellIdentifier", fetchRequest: request, mainContext: self.dataStack!.mainContext, configuration: { cell, item, indexPath in

        if let cell = cell as? NotificationsTableViewCell {
            cell.TitleLabel.text = item.value(forKey: "title") as? String
            cell.RequestIdLabel.text = item.value(forKey: "request_id") as? String!
            cell.RequestTimeLabel.text = item.value(forKey: "requestTime") as? String!
        }
    })
    return dataSource
}()

Thanks in advance!!

3lvis commented 7 years ago

@swabzz There's a demo showing using DATASource with custom UITableViewCells. Check it out :)

https://github.com/SyncDB/DATASource/blob/master/SwiftDemo/TableViewController/TableViewController.swift

swabzz commented 7 years ago

Thanks for quick reply @3lvis

I tried same but getting same error

fatal error: unexpectedly found nil while unwrapping an Optional value

My custom cell

class NotificationsTableViewCell: UITableViewCell {

@IBOutlet weak var NotificationIcon: UIImageView!
@IBOutlet weak var TitleLabel: UILabel!
@IBOutlet weak var RequestIdLabel: UILabel!
@IBOutlet weak var RequestTimeLabel: UILabel!

func configureItem(item: Requests) {
    TitleLabel.text = item.title
    RequestIdLabel.text = "\(item.request_id)"
    RequestTimeLabel.text = item.nCode
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if selected {
        setSelected(false, animated: false)
    }
}

}

3lvis commented 7 years ago

@swabzz Hi, it's most likely something else then. Any chance I can get a sample project to reproduce this one?

swabzz commented 7 years ago

Okay here is the code then,

import DATAStack import DATASource

//UIViewController class NotificationsViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

// MARK: Private
var dataStack: DATAStack!

lazy var dataSource: DATASource = {
    let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Requests")
    request.sortDescriptors = [NSSortDescriptor(key: "request_id", ascending: true)]

    let dataSource = DATASource(tableView: self.tableView, cellIdentifier: "NotificationsTableViewCellIdentifier", fetchRequest: request, mainContext: self.dataStack!.mainContext, configuration: { cell, item, indexPath in

        let cell = cell as! NotificationsTableViewCell
        cell.TitleLabel.text = item.value(forKey: "title") as? String
        cell.RequestIdLabel.text = item.value(forKey: "request_id") as? String
        cell.RequestTimeLabel.text = item.value(forKey: "requestTime") as? String

    })
    return dataSource
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataStack = DATAStack(modelName: "xenian")
    self.tableView.register(NotificationsTableViewCell.self, forCellReuseIdentifier: "NotificationsTableViewCellIdentifier")
    self.tableView.dataSource = self.dataSource
}

}

//CUSTOM CELL

class NotificationsTableViewCell: UITableViewCell {

@IBOutlet weak var NotificationIcon: UIImageView! @IBOutlet weak var TitleLabel: UILabel! @IBOutlet weak var RequestIdLabel: UILabel! @IBOutlet weak var RequestTimeLabel: UILabel!

func configureItem(item: Requests) { TitleLabel.text = item.title RequestIdLabel.text = "(item.request_id)" RequestTimeLabel.text = item.nCode }

override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { setSelected(false, animated: false) } } }

CAN YOU REPRODUCE IT USING ABOVE CODE PLEASE???

3lvis commented 7 years ago

Hi @swabzz,

I'm afraid that doesn't help. I can't find a way to reproduce the crash with that code. Could you send me a Xcode project that I can use?

swabzz commented 7 years ago

Can you check in above project where i am missing proper coding please?? sharing full project is just impossible for me its huge lines of code :-)

swabzz commented 7 years ago

Hi,

I gone through your demo here .. https://github.com/SyncDB/DATASource/tree/master/SwiftDemo you are not using storyboard i think.

is it possible to use library with storyboard custom uicollection view subclass???

swabzz commented 7 years ago

Hello @3lvis

Sharing sample project please take a look testDATASource.zip

3lvis commented 7 years ago

Hi @swabzz,

Thanks for the sample project, it was very helpful.

Remove the cell registration from viewDidLoad and it works fine

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataStack = DATAStack(modelName: "testDATASource")
        self.tableView.dataSource = self.dataSource
    }
3lvis commented 7 years ago

You also need to remove the override of init?(coder).

import UIKit

class NotificationsTableViewCell: UITableViewCell {

    public static let Identifier = "NotificationsTableViewCellIdentifier"

    @IBOutlet weak var NotificationIcon: UIImageView!
    @IBOutlet weak var TitleLabel: UILabel!
    @IBOutlet weak var RequestIdLabel: UILabel!
    @IBOutlet weak var RequestTimeLabel: UILabel!
}
3lvis commented 7 years ago

Here's the working project.

FixedtestDATASource.zip

3lvis commented 7 years ago

I hope that was useful! Have a nice week 😁

swabzz commented 7 years ago

👍 Working Thanks for help @3lvis