rushisangani / RSSelectionMenu

An elegant selection list or dropdown menu for iOS with single or multiple selections.
MIT License
351 stars 91 forks source link

Every row mark as checked #30

Closed proovek closed 6 years ago

proovek commented 6 years ago

What is my problem? - It's works perfect but when I select one row, the App shows me "one row selected... etc" and it's okay, but when im looking into an App I have "checked" every rows.

there is my code:

@IBAction func licenseSelection(_ sender: Any) {
                let context = licenseSavingController.persistentContainer.viewContext
                let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Licencja")
            let selectionMenu = RSSelectionMenu(selectionType: .Single, dataSource: licencje, cellType: .Basic) { (cell, object, indexPath) in
                cell.tintColor = .orange

                request.returnsObjectsAsFaults = false
                do {
                    var result = try context.fetch(request) as! [NSManagedObject]
                    result = [result[indexPath.row]]
                    for data in result {
                        self.licznik = self.licznik+1
                        cell.textLabel?.text = data.value(forKey: "licenseDescript") as? String ?? data.value(forKey: "licenseURL") as? String
                        //forKey: "licenseURL") as? String ?? "default"
                        //forKey: "licenseNumber") as? String ?? "default"
                        //data.value(forKey: "deviceID") as? String ?? "default"
                    }
                } catch {
                    print("Failed")
            }
        }

            selectionMenu.uniquePropertyName = "licznik"
            selectionMenu.setSelectedItems(items: wybranaLicencja) { (text, isSelected, selectedItems) in
                self.wybranaLicencja = selectedItems
                print(selectedItems)
            }
            selectionMenu.show(style: .Popover(sourceView: sender as! UIView, size: nil), from: self)
        }

like that: https://imgur.com/nveTKJ8 Im really confused with this :P

proovek commented 6 years ago

And how I can get some values from dataCore which is selected?

rushisangani commented 6 years ago

Your data models must have one unique property. Here, In your Entity Licencja which property has uniqueValue? You need to set "String description of the property"

proovek commented 6 years ago

Every value in this entity is unique :P for example licenseNumber is unique

proovek commented 6 years ago

Can you help me with this? Im trying sooo many times :/

rushisangani commented 6 years ago

Then write selectionMenu.uniquePropertyName = "licenseNumber"

rushisangani commented 6 years ago

Maybe you can try something like this

let context = licenseSavingController.persistentContainer.viewContext
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Licencja")
        request.returnsObjectsAsFaults = false

        guard let result = try? context.fetch(request) as? [Licencja] else {
            return
        }

        let selectionMenu = RSSelectionMenu(selectionType: .Single, dataSource: result ?? [], cellType: .Basic) { (cell, object, indexPath) in

            let licznik = object
            cell.tintColor = .orange
            cell.textLabel?.text = licznik.licenseDescript
        }
proovek commented 6 years ago

I'll try later but thanks for some help :) Now im on outside

proovek commented 6 years ago

I try and it's doing the same :/ I have a problem with uniquePropertyName

proovek commented 6 years ago

Every marks are checked every time... it's weird

rushisangani commented 6 years ago

Can you please share your code?

proovek commented 6 years ago

All my code? For explain - Your library works good, but my problem is here - I need to set uniquePropertyName with my License entity for example for attribute with id = "licenseNumber" I need to recall to this id, but I couldn't get this id from dataCore

proovek commented 6 years ago

response from entity [<Licencja: 0x6000002843d0> (entity: Licencja; id: 0xd000000000040000 <x-coredata://197130E8-8765-4C43-A0E1-2628597E0693/Licencja/p1> ; data: { deviceID = "0C4BE11B-63F5-43C5-899F-DF80F319EFD4"; licenseDescript = "Serwer Api testowy"; licenseNumber = "DED-3139557474104099-API"; licenseURL = "https://someexampleAPI.com.pl/api/"; })]

what I should to use to get "id: 0xd000000..."??? I need this for a unique key and it should works

rushisangani commented 6 years ago

Your Entity must be subclass of NSObject.

proovek commented 6 years ago

that's how im sending someone to entity,

import Foundation
import CoreData

extension Licencja {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Licencja> {
        return NSFetchRequest<Licencja>(entityName: "Licencja")
    }

    @NSManaged public var licenseURL: String?
    @NSManaged public var deviceID: String?
    @NSManaged public var licenseNumber: String?
    @NSManaged public var licenseDescript: String?
}
proovek commented 6 years ago

What do you mean? Can you write some example or something?

proovek commented 6 years ago

My entity is a subclasss of NSManagedObject it's the same as nsobject

proovek commented 6 years ago

How can I delete those little "birds" on selected?

proovek commented 6 years ago

Only they works badly

rushisangani commented 6 years ago

You need to perform following steps:

  1. Implement UniqueProperty protocol in your Licencja class.
  2. Return property name that has unique value.
func uniquePropertyName() -> String {
        return "licenseNumber"
}
  1. Fetch your entity Licencja list.

let result = <** Fetch call here **>

  1. Declare already selected items to show in menu.

var selectedLicencja = []()

  1. Implement selection menu as follows:
let selectionMenu =  RSSelectionMenu(dataSource: result ?? []) { (cell, object, indexPath) in
    cell.textLabel?.text = object. licenseDescript
 }

selectionMenu.setSelectedItems(items: selectedLicencja) { (text, isSelected, selectedItems) in

    // update your existing array with updated selected items, so when menu presents second time updated items will be default selected.
    self.selectedLicencja = selectedItems
}

// show as popover
selectionMenu.show(style: .Popover(sourceView: sender as! UIView, size: nil), from: self)
proovek commented 6 years ago

I do all points, I implement unique...

@objc(Licencja)
public class Licencja: NSManagedObject{

    func uniquePropertyName() -> String {
        return "licenseNumber"
    }

}

i have an selectedlicense declaration like that... var wybranaLicencja = [Licencja]()

also I have fetch entity

guard let result = try? contextMark.fetch(requestMark) as? [Licencja] else {
            return
        }

        let selectionMark = RSSelectionMenu(selectionType: .Single, dataSource: result ?? [], cellType: .Basic) { (cell, object, indexPath) in

            let licznik = object
            cell.tintColor = .orange
            cell.textLabel?.text = licznik.licenseDescript ?? licznik.licenseURL
        }

also I do selected item..

selectionMark.setSelectedItems(items: wybranaLicencja) { (text, isSelected, selectedItems) in
            self.wybranaLicencja = selectedItems

And it's okay, it's works, but when im select some one cell everything is good - im printing this license and it's good, im using it as variable. nvm, my problem is here - when I choose a cell, the stamp appears with everyone like that: https://imgur.com/a/gaRnd8I

two marks... ;v

proovek commented 6 years ago

Can you tell me how to delete marks from your code?

rushisangani commented 6 years ago

It should work perfectly if you've configured all things properly. I'll get idea if you share your datamodels and other files where you're showing the menu.

proovek commented 6 years ago

I'll sent you my project on email, I don't want to share it public right now, okay?

/edit Look gmail, my god :)

rushisangani commented 6 years ago

Can you please try updating pod with version 4.0.2 and check if it works?

proovek commented 6 years ago

Yea I can try

proovek commented 6 years ago

Now it's work fine, thanks for helping me.