这是代码为mvc的方法吗?
模型: -
typealias JSONDictionary = [String:Any]
class EventModel: NSObject {
var name :String!
init?(dictionary :JSONDictionary) {
guard let name = dictionary["name"] as? String else{
return
}
self.name = name
}
}
datasourcemodel: -
class EventDataSourceModel: NSObject {
var dataListArray:Array<EventModel>? = []
init(array :Array<[String:Any]>?) {
super.init()
var newArray:Array<[String:Any]> = []
if array == nil{
newArray = self.getJsonDataStored2()
}
else{
newArray = array!
}
var datalist:Array<EventModel> = []
for dict in newArray{
let model = EventModel(dictionary: dict)
datalist.append(model!)
}
self.dataListArray = datalist
}
}
typealias dummyDataSource2 = EventDataSourceModel
extension dummyDataSource2{
func getJsonDataStored2() ->Array<Dictionary<String,String>>{
let jsonArray = [["id":"201","name":"Dosa Fest","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Shiraz Garden"],["id":"202","name":"Organic Vegan Fest","imageurl":"","location":"Doha,Qatar","date":"15 Jan 2018 30 Jan 2018","place":"Evergreen Organics"],["id":"203","name":"Food Of Life Time","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Turkey Central"],["id":"204","name":"Tea Time","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Doha Food Mall"],["id":"205","name":"Dosa Fest","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Shiraz Garden"],["id":"206","name":"Organic Vegan Fest","imageurl":"","location":"Doha,Qatar","date":"15 Jan 2018 30 Jan 2018","place":"Evergreen Organics"],["id":"207","name":"Food Of Life Time","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Turkey Central"],["id":"208","name":"Tea Time","imageurl":"","location":"Doha,Qatar","date":"Jan 2018","place":"Doha Food Mall"]] as Array<Dictionary<String,String>>
return jsonArray
}
}
控制器: -
class EventViewController: UIViewController ,UITableViewDataSource{
@IBOutlet private weak var tableView: UITableView!
var datasourceModel:EventDataSourceModel!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withdatasourcemodel datasource:EventDataSourceModel) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
datasourceModel = datasource
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (datasourceModel.dataListArray?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "eventcell"
var cell: EventCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EventCell
if cell == nil {
tableView.register(UINib(nibName: "EventCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EventCell
}
cell.setEventData(events:datasourceModel.dataListArray![indexPath.row])
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
终于查看: -
class QM_EventCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var location: UILabel!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var imageurl: UILabel!
@IBOutlet weak var place: UILabel!
@IBOutlet weak var images: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setEventData(events:QM_EventModel)
{
self.name.text = events.name
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我是这个领域的新手。我是自己学习的。所以我不知道方法做得对还是错。我需要用mvc来开发项目。因为我在很多网站上都提到了使用swift的mvc方法.i知道要在mvvm中做,但是如何改变mvc会让我有点混乱。 这种方式需要分开。如果错误请告诉我该怎么做