Coder Social home page Coder Social logo

rxswiftmvvm's Introduction

rxswiftmvvm's People

Contributors

jianweiwangs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rxswiftmvvm's Issues

为什么列表没任何展示呢?纳闷了

我的代码如下,但是列表就是没有数据展示?实在不解

class ViewController: UIViewController{
    
    var viewModel : MyViewModel? = MyViewModel()
    let disposeBag = DisposeBag()
    
    var tableView : UITableView! = {
        
       var tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        tableView.rowHeight     = 44.0
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.tableFooterView = UIView()
     
        return tableView
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
      
        
        self.viewModel?
            .homeAction?
            .execute(())
            .bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)){
                index, model, cell in

                cell.textLabel?.text = model.nick_name
            }
            //.disposed(by:disposeBag)
        
    }


   var homeAction: Action<Void, [BaseUser]>?
    let model = Model()
    var data: [BaseUser]?
    
    init() {
        
        initialize()
    }
    
    
    func initialize() {
        
        homeAction = Action<Void, [BaseUser]>.init(workFactory: {[unowned self] in
            
            var params : Dictionary<String, Any> = [:]
            params["lp"]            = ""
            params["page"]          = "1"
            params["age_min"]       = "-1"
            params["age_max"]       = "-1"
            params["height_min"]    = "-1"
            params["height_max"]    = "-1"
            params["prov"]          = ""
            params["city"]          = ""
            params["user_id"]       = "1"
            
            
           return self.model.request(UserAPI.homeList(params), Msg<BaseUser>.self)
                    .debug()
                    .map{$0.list ?? []}
                    .do(onNext: {
                        self.data = $0
                    })
        })

请教一个问题HandyJSON解析的问题

接口中除了解析到[Storie]数组外,一般接口中都返回code,msg等额外信息,如何解析呢?每个接口都弄一个包含相同code,msg+额外Obj,感觉这样也不现实。

项目中最外层结构大致如下:

class Msg : HandyJSON {
    
    var code : Int?   = 0
    var msg : String?  = ""
    var obj : HandyJSON?
    var list : [HandyJSON]?
    
    required init() {
        
    }
}
  homeAction = Action<Void, [Storie]>.init(workFactory: {[unowned self] in
            self.model.request(API.Main, FirstModel.self)
                .map{$0.stories ?? []}
                .do(onNext: {
                    self.data = $0
                })
        })
extension Observable {
    func map<T: HandyJSON>(_ type: T.Type) -> Observable<T> {
        return map { response in
            guard let response = response as? Moya.Response else {
                throw RxSwiftMoyaError.ResponseError
            }
            
            guard (200...209) ~= response.statusCode else {
                throw RxSwiftMoyaError.RequestFailedError
            }
            
            guard let json = try? JSONSerialization.jsonObject(with: response.data, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: Any] else {
                throw RxSwiftMoyaError.ResponseError
            }
            
            let object = JSONDeserializer<T>.deserializeFrom(dict: json)
            guard let model = object else {
                throw RxSwiftMoyaError.ParseJSONError
            }
            return model
        }
    }
}

列表翻页时,如何追加数据呢?

请教翻页问题:想实现列表翻页功能,但是接口返回的list 如何追加到self.data上呢?如何将追加的最新列表数据显示到列表上呢?

  homeAction = Action<Void, Msg<BaseUser>>.init(workFactory: {[unowned self] in
            
            var params : Dictionary<String, Any> = [:]
            params["lp"]            = self.lp
            params["page"]          = "1"
            params["age_min"]       = "-1"
            params["age_max"]       = "-1"
            params["height_min"]    = "-1"
            params["height_max"]    = "-1"
            params["prov"]          = ""
            params["city"]          = ""
            params["user_id"]       = "1"
            
            
            return self.model.request(UserAPI.homeList(params), Msg<BaseUser>.self)
                    .debug()
                    .filter({ [unowned self] (value) -> Bool in
                        
                        Loger.log(msg: "\(value.code!) \(value.msg!)")
                        if value.code == 1 {
                            self.lp = value.lp ?? ""
                            self.refreshStatus.value = RefreshStatus.DropDownSuccess
                            
                            //处理返回的数据,给dataSource赋值
                            if (value.list?.count)! >= 0 {
                                self.refreshStatus.value = RefreshStatus.PullSuccessHasMoreData
                            } else {
                                self.refreshStatus.value = RefreshStatus.PullSuccessNoMoreData
                            }
                            
                            self.data = value.list ?? []
                            return true
                        }
                        
                        
                        self.refreshStatus.value = RefreshStatus.DropDownSuccess
                        
                        return true
                        
                    })
        })
self.viewModel?
            .homeAction?
            .execute(())
            .filter({ (value) -> Bool in
                
                Loger.log(msg: "\(value.code!) \(value.msg!)")
                if value.code == 1
                {
                    return true
                }
                return false
                
            })
            .map{$0.list ?? []}
            .bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)){
                index, model, cell in
                
                cell.textLabel?.text = model.nick_name
            }
            .disposed(by:disposeBag)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.