一·Model层
OC : Model层数据如果是层层嵌套我会用以下的方法来分割
代码语言:javascript复制@interface levelTwoModel : NSObject
//二级数据
@property (nonatomic, strong) NSString *iconUrl;
@property (nonatomic, strong) NSString *className;
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString *name;
@end
@interface levelOneModel : NSObject
//一级数据
@property (nonatomic, strong) NSArray <levelTwoModel *>*levelTwo;
@property (nonatomic, strong) NSString *iconUrl;
@property (nonatomic, strong) NSString *className;
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString *name;
@end
Swift :
代码语言:javascript复制struct levelModel: Codable {
var levelTwoModel : [levelTwo]
var iconUrl:String
var className:String
var id:Ind
var name:String
}
struct levelTwo{
var iconUrl:String
var className:String
var id:Int
var name:String
}
二·ViewController层
OC : 在Objective-C 我会在Interface 声明一个Model变量通过Setter的方法把Model层数据传输过去
拿我在以前写的一篇文章中作例子 iOS-NSArray与Model模型 该篇文章中Model层数据通过UITabelView的delegate & dataSource 方法
代码语言:javascript复制-(UITableViewCell *)cellForRowAtIndexPath {
·····
yourModel *model = _dataArr[indexPath.row];
return cell
}
那么Model数据通过Delegate传输到UITableViewCell当中去调用Setter来刷新数据
Swift :
代码语言:javascript复制class ViewController: UIViewController, UITabelViewDelegate, UITableViewDataSource, levelPresenterDelegate {
private let tableView : UITabelView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier:"cell")
return table
} // UITabel的复用在私有方法中
//Model层私有变量
private var model = [levelModel]()
//Presenter层
private let presenter = modelPresenter()
override func viewDidLoad() {
super.viewDidLoad()
title = "Users"
view.addSubview(tableView)
//tabel
tableView.delegate = self
tableView.dataSource = self
//presenter
presenter.setViewDelegate(delegate: self)
presenter.getUsers()
}
//UITabelViewDataSource & Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = users[indexPath.row].name
cell.contentConfiguration = content
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//告诉生产者去刷新这次点击cell的操作
}
//persenter delegate
func presentLevelOneModel(model: [levelModel]) {
self.model = model
DispatchQueue.main.async {
self.tableView.reloadData() //异步刷新
}
}
func presentAlert(title: String, message: String) {
}
}
三·Controller层 & Presenter层
OC :
代码语言:javascript复制- (void)setModel:(Model *)model{
_model = model;
}
Swift : 在Swift的MVP架构模式中P->Presenter是通过Protocol协议来传输数据
代码语言:javascript复制protocol levelPresenterDelegate : AnyObject {
func presentLevelOneModel(model : [levelModel])
func presentAlert(title: String, message: String)
}
我们可以通过类型声明的方法
typealias PresenterDelegate = levelPresenterDelegate & UIViewController
class modelPresenter{
weak var delegate: PresenterDelegate? //遵循我们声明的levelPresenterDelegate 和 UIViewController协议
public func getModel() {
guard let url = URL(string: "https://cloud.tencent.com/developer/user/5302716") else { return }
_ = URLSession.shared.dataTask(with: url) { [weak self]data, _, error in
guard let data = data, error == nil else {
return
}
do {
let model = try JSONDecoder().decode([levelModel].self, from: data)
self?.delegate?.presentLevelOneModel(model: model)
}
catch {
print(error)
}
}
}
public func setViewDelegate(delegate: PresenterDelegate) {
self.delegate = delegate
}
}
}