博客> swift MVVM
swift MVVM
2017-06-25 05:15 评论:0 阅读:199 vipsamuel
swift MVVM

1.C

设置addSubview()

2.V

定义所哟的UIKit

3.VM

处理一些v层的逻辑 引用target的时候一定要加weak

class ViewController: UIViewController {

var tableView = TestTableView()
var rightButton = TestBarItem()
override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(tableView)
    tableView.snp_makeConstraints { (make) in
        make.edges.equalTo(self.view)
    }
    self.navigationItem.rightBarButtonItem = rightButton
    rightButton.targetController = self
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
//实现方法
override func viewDidLayoutSubviews() {
    self.tableView.separatorInset = UIEdgeInsetsZero
    self.tableView.layoutMargins = UIEdgeInsetsZero
}

}

class TestTableView: UITableView {

var vm = TestTableViewModel()
init(){
    super.init(frame: CGRect(), style: .Plain)
    self.delegate = vm
    self.dataSource = vm
    //取消掉分割线
    self.tableFooterView = UIView()
    //自动计算cell高度
    self.estimatedRowHeight = 60
    self.rowHeight = UITableViewAutomaticDimension
    self.registerClass(TestTableViewCell.self, forCellReuseIdentifier: "testCell")
    vm.target = self
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class TestBarItem: UIBarButtonItem {

var vm = TestBarItemViewModel()
//View 持有 Controller
weak var targetController: ViewController!
override init(){
    super.init()
    self.title = "点击"
    self.style = .Plain
    self.target = vm;
    self.action = #selector(vm.testAction)
    vm.target = self
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class TestTableViewModel: NSObject,UITableViewDelegate,UITableViewDataSource {

var target: TestTableView!
//点击展开、合并
var dict: Dictionary<String> = [:]
var textValue: NSString = "我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n我是一个UILabel()\n"
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("testCell", forIndexPath: indexPath) as! TestTableViewCell
    cell.initCell(textValue as String)
    if dict[String(indexPath.row)] == "0" {
        cell.label.numberOfLines = 0
    } else {
        cell.label.numberOfLines = 1
    }
    print(dict)
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    let label = cell?.contentView.viewWithTag(1000) as! UILabel
    //点击展开、合并
    tableView.beginUpdates()
    if label.numberOfLines == 0 {
        label.numberOfLines = 1
        dict[String(indexPath.row)] = "1"
    } else {
        label.numberOfLines = 0
        dict[String(indexPath.row)] = "0"
    }
    print(dict)
    tableView.endUpdates()
}
//回调 cell回调 滚动屏幕cell将要显示的时候调用
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
}

}

//持有vm 处理一些v层的逻辑 //引用target的时候一定要加weak class TestBarItemViewModel: NSObject {

//VM层需要View
weak var target: TestBarItem!
func testAction() -> Void {
    print("I'm cliented")
    let vc = SecondViewController()
    target.targetController.navigationController?.pushViewController(vc, animated: true)
}

}

收藏
0
sina weixin mail 回到顶部