Eureka: Rating cell

Created on 9 Oct 2017  路  3Comments  路  Source: xmartlabs/Eureka

Is possible with this pod create custom row? I need cell for rating (some kind of 5 stars view)

Most helpful comment

yes, its posible, i use this pod https://github.com/evgenyneu/Cosmos and works, create your .xib and this code

import UIKit
import Cosmos
import Eureka
open class CustomRateCell: Cell<Int>, CellType {
    private var awakeFromNibCalled = false
    @IBOutlet weak var cosmosRating: CosmosView!
    @IBOutlet open weak var titleLabel: UILabel!

    public required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
    }

    deinit {
        guard !awakeFromNibCalled else { return }
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIContentSizeCategoryDidChange, object: nil)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        awakeFromNibCalled = true
    }

    open override func setup() {
        super.setup()

        cosmosRating.didTouchCosmos = valueChanged
        cosmosRating.didFinishTouchingCosmos = valueChanged

        selectionStyle = .none
        //custom settings for cosmos
        cosmosRating.settings.starSize = 30.0
        cosmosRating.settings.emptyBorderWidth = 1
        cosmosRating.settings.filledBorderWidth = 5
    }

    func valueChanged(_ rating: Double) {
        row.value = Int(rating)
        row.updateCell()
    }

    open override func update() {
        titleLabel.text = row.title
        row.value = Int(cosmosRating.rating)
    }

    private var customRateRow: CustomRateRow {
        return row as! CustomRateRow
    }
}
public final class CustomRateRow: Row<CustomRateCell>, RowType {

    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<CustomRateCell>(nibName: "SliderCell", bundle: Bundle.main)
    }
}

and calling something like this

<<< CustomRateRow() { 
    $0.cell.height = { 69 }
    $0.cell.cosmosRating.rating = 0.0
    $0.cell.cosmosRating.settings.totalStars =  10
}.onRowValidationChanged { cell, row in
    let rowIndex = row.indexPath!.row
    while row.section!.count > rowIndex + 1 && row.section?[rowIndex  + 1] is LabelRow {
        row.section?.remove(at: rowIndex + 1)
    }
    if !row.isValid {
        for (index, _) in row.validationErrors.map({ $0.msg }).enumerated() {
            let labelRow = LabelRow() {
                $0.title = "this field is required"
                $0.cell.height = { 35 }
            }
            row.section?.insert(labelRow, at: row.indexPath!.row + index + 1)
        }
    }
}

All 3 comments

yes, its posible, i use this pod https://github.com/evgenyneu/Cosmos and works, create your .xib and this code

import UIKit
import Cosmos
import Eureka
open class CustomRateCell: Cell<Int>, CellType {
    private var awakeFromNibCalled = false
    @IBOutlet weak var cosmosRating: CosmosView!
    @IBOutlet open weak var titleLabel: UILabel!

    public required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
    }

    deinit {
        guard !awakeFromNibCalled else { return }
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIContentSizeCategoryDidChange, object: nil)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        awakeFromNibCalled = true
    }

    open override func setup() {
        super.setup()

        cosmosRating.didTouchCosmos = valueChanged
        cosmosRating.didFinishTouchingCosmos = valueChanged

        selectionStyle = .none
        //custom settings for cosmos
        cosmosRating.settings.starSize = 30.0
        cosmosRating.settings.emptyBorderWidth = 1
        cosmosRating.settings.filledBorderWidth = 5
    }

    func valueChanged(_ rating: Double) {
        row.value = Int(rating)
        row.updateCell()
    }

    open override func update() {
        titleLabel.text = row.title
        row.value = Int(cosmosRating.rating)
    }

    private var customRateRow: CustomRateRow {
        return row as! CustomRateRow
    }
}
public final class CustomRateRow: Row<CustomRateCell>, RowType {

    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<CustomRateCell>(nibName: "SliderCell", bundle: Bundle.main)
    }
}

and calling something like this

<<< CustomRateRow() { 
    $0.cell.height = { 69 }
    $0.cell.cosmosRating.rating = 0.0
    $0.cell.cosmosRating.settings.totalStars =  10
}.onRowValidationChanged { cell, row in
    let rowIndex = row.indexPath!.row
    while row.section!.count > rowIndex + 1 && row.section?[rowIndex  + 1] is LabelRow {
        row.section?.remove(at: rowIndex + 1)
    }
    if !row.isValid {
        for (index, _) in row.validationErrors.map({ $0.msg }).enumerated() {
            let labelRow = LabelRow() {
                $0.title = "this field is required"
                $0.cell.height = { 35 }
            }
            row.section?.insert(labelRow, at: row.indexPath!.row + index + 1)
        }
    }
}

thank you

Thanks @elgartoinf for the example code, would be really great if you upload your custom row to Eureka Community! It may help a lot of people with the same requirement.

I'm closing this issue

Was this page helpful?
0 / 5 - 0 ratings