PushRow 与 Eureka 表单的自定义对象

Posted

技术标签:

【中文标题】PushRow 与 Eureka 表单的自定义对象【英文标题】:PushRow With Custom Object With Eureka Forms 【发布时间】:2016-07-18 23:03:02 【问题描述】:

我无法在 Eureka 表单中使用自定义对象,我正在尝试从 Rest 服务加载对象,我正在使用 Alamofire 获取选项,我正在使用 row.options.append 来填充数据,当我选择单元格时出现错误断言失败:重复标记构建

这是我的符合 Equatable 协议的课程

class Building: Mappable, Equatable 
   var name_building: String?
   var id_building: Int?

   required init?()
   

   required init?(_ map: Map)   
   

   func mapping(map: Map) 
      id_building <- map["id_building"]
      name_building <- map["name_building"]
   


func == (lhs: Building, rhs: Building) -> Bool 
   return lhs.id_building == rhs.id_building

这是表格

import UIKit
import Eureka
import CoreLocation
import Alamofire
import KeychainAccess
import ObjectMapper
import SnapKit

class UserViewController: FormViewController 


    override func viewDidLoad() 
        super.viewDidLoad()


        self.title = "Perfil del Usuario"

        // Do any additional setup after loading the view.
        form +++ Section("Datos Generales")
            <<< NameFloatLabelRow("kName") 
                $0.title = "Nombre"
            
            <<< TextFloatLabelRow("kLastName") 
                $0.title = "Apellidos"
            
            <<< TextFloatLabelRow("kPhone") 
                $0.title = "Teléfono"
            
            <<< TextFloatLabelRow("kEmail") 
                $0.title = "Email"
                $0.disabled = true;
            
            <<< SegmentedRow<String>("kGenre") 
                $0.title = "Genero"
                $0.options = ["Masculino", "Femenino"]
            
            <<< CheckRow("kRider") 
                $0.title = "¿Estoy dispuesto a dar Aventones?"
                $0.value = true
            

            +++ Section("Casa")
            <<< LocationRow("kHome")
                $0.title = "Casa"
                $0.value = CLLocation(latitude: -34.91, longitude: -56.1646)
            
            <<< TimeInlineRow("kHomeDepartureTime")
                $0.title = "Hora de Salida"
                $0.value = NSDate()
            
            +++ Section("Trabajo")
            <<< PushRow<Building>("kBuilding") 
                $0.title = "Edificio"
                $0.selectorTitle = "Donde Trabajas?"
            
            <<< TimeInlineRow("kEnterTime")
                $0.title = "Hora de Entrada"
                $0.value = NSDate()
            
            <<< TimeInlineRow("kExitTime")
                $0.title = "Hora de Salida"
                $0.value = NSDate()
            
            +++ Section()
            <<< ButtonRow()  (row: ButtonRow) -> Void in
                row.title = "GUARDAR"
                  .onCellSelection( (cell, row) in
                    print("Salvando Informacion")
                    let nameRow: NameFloatLabelRow! = self.form.rowByTag("kName")
                    let lastNameRow: TextFloatLabelRow! = self.form.rowByTag("kLastName")
                    let phoneRow: TextFloatLabelRow! = self.form.rowByTag("kPhone")
                    let emailRow: TextFloatLabelRow! = self.form.rowByTag("kEmail")
                    let genreRow: SegmentedRow<String>! = self.form.rowByTag("kGenre")
                    let riderRow: CheckRow! = self.form.rowByTag("kRider")
                    let locationRow: LocationRow! = self.form.rowByTag("kHome")
                    let homeDepartureTimeRow: TimeInlineRow! = self.form.rowByTag("kHomeDepartureTime")
                    let buildingRow: PushRow<Building>! = self.form.rowByTag("kBuilding")
                    let buildingEnterTimeRow: TimeInlineRow! = self.form.rowByTag("kEnterTime")
                    let buildingExitTimeRow: TimeInlineRow! = self.form.rowByTag("kExitTime")

                    let user = User()

                    user.email = emailRow.value
                    user.name = nameRow.value
                    user.lastName = lastNameRow.value
                    user.genre = genreRow.value
                    user.phone = phoneRow.value
                    user.id_type = riderRow.value == true ? 1 : 2
                    user.homeDepartureDate = homeDepartureTimeRow.value
                    user.jobEnterTime = buildingEnterTimeRow.value
                    user.jobExitTime = buildingExitTimeRow.value
                    user.id_building = 3

                    let location = locationRow.value! as CLLocation
                    user.latitude = location.coordinate.latitude
                    user.longitude = location.coordinate.longitude

                    let JSONString = Mapper().toJSONString(user, prettyPrint: false)
                    let mappedObject = Mapper<User>().map(JSONString)


                    let request = "requesttoCreateUser"

                    Alamofire.request(.POST, request, parameters: Mapper().toJSON(user), encoding: .JSON)
                        .validate()
                        .responseJSON  response in
                            switch response.result 
                            case .Success:
                                print("Validation Successful")
                            case .Failure(let error):
                                print(error)
                            
                     

                )

        self.getBuilding();

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func getBuilding()
        let buildingRow: PushRow<Building>! = self.form.rowByTag("kBuilding")

        let request = "requestToGetBuildings"

        Alamofire.request(.GET, request).responseArray  (response: Response<[Building], NSError>) in

            let buildingsArray = response.result.value

            if let buildingsArray = buildingsArray 
                for building in buildingsArray 
                    buildingRow.options.append(building)
                
            
        
    


它在 Eureka 类 BaseRow.swift 中崩溃了

    final func wasAddedToFormInSection(section: Section) 
        self.section = section
        if let t = tag 
            assert(section.form?.rowsByTag[t] == nil, "Duplicate tag \(t)")
            self.section?.form?.rowsByTag[t] = self
            self.section?.form?.tagToValues[t] = baseValue as? AnyObject ?? NSNull()
        
        addToRowObservers()
        evaluateHidden()
        evaluateDisabled()
    

【问题讨论】:

通常,当您有另一个带有"kName""kLastName" 或“kBuilding”的标签时会发生这种情况,因为错误显示“重复标签”您可以检查您是否不添加另一个带有任何这个名字? 不,我没有任何重复的标签,我已经检查了两次 你能贴出你所有的viewController代码吗? 我刚刚更新了帖子以添加 UserViewController 的完整代码 @Angel,能解决这个问题吗?另外,是否可以使用自定义类型作为PushRow 的选项?例如,NSManagedObject 子类? 【参考方案1】:

正如提到的here。

您需要做的就是使您的模型符合CustomStringConvertible 协议。

这是因为 SelectorViewController 的默认实现 使用 String(option) 作为 PushRow 行的标记

所以你的班级应该是这样的:

class Building: Mappable, Equatable,CustomStringConvertible 
   var name_building: String?
   var id_building: Int?
   var description: String 

        return name_building
    
   required init?()
   

   required init?(_ map: Map)   
   

   func mapping(map: Map) 
      id_building <- map["id_building"]
      name_building <- map["name_building"]
   


func == (lhs: Building, rhs: Building) -> Bool 
   return lhs.id_building == rhs.id_building

【讨论】:

以上是关于PushRow 与 Eureka 表单的自定义对象的主要内容,如果未能解决你的问题,请参考以下文章

为一行动态加载选项

将自己的自定义表单与 MailChimp 一起使用 [关闭]

嵌套的自定义 FormArray 组件不与具有 FormArrayName 的子表单绑定

Magento 中的自定义表单

如何使用 iText 获取 Pdf 表单字段的自定义格式脚本?

Laravel 5.5 的自定义验证对象/类