Giter VIP home page Giter VIP logo

duytableviewcontroller's Introduction

DUYTableViewController

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

DUYTableViewController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'DUYTableViewController'

Step 1:

Add this line at the top of your swift file

import DUYTableViewController

Step 2:

Create your ViewController View Model, here is where you store your view data, we have an example here:

class TestTableViewModel : DUYTableviewViewModel{
    var dataImageTitle : [(String,String,String)] = [
        ("Justice league 1", "Best movie in 2018", "justice"),
        ("Hello Scene", "The most breathtaking scene in 2019", "scene"),
        ("Justice league 1", "Best movie in 2018", "news")
    ]
    
    var dataBasic : [(String, String)] = [
        ("Cruise line makes claim about granddad holding tot before she fell off ship","Royal Caribbean is seeking to dismiss a civil lawsuit filed by the family of Chloe Wiegand, claiming that her grandfather"),
        ("GRANDFATHER WHO DROPPED TODDLER SPEAKS OUT FOLLOWING TRAGIC INCIDENT","An attorney for the Wiegand family said later that month that Anello had held Chloe up to what he believed to be a window, so she could bang on the glass")
    ]
    
    var dataCollection : [(String,String,[String])] = [
        ("The death of the blockbuster: How The Rise of Skywalker marks the end of a cinematic era", "With Star Wars in trouble and Marvel at a turning point, and X-Men, Men in Black and Terminator among the established franchises faltering in 2019, Adam White asks what’s next for the traditional blockbuster", ["scene", "justice"]),
        ("These Colleges Don’t Make The Grade","The Universities That Just Aren’t Worth The Tuition",["news", "scene"]),
        ("$120K Banana Makes For An Expensive Snack","Typically, bananas are one of nature’s most nutritious, delicious, and affordable snacks. At the grocery store",["justice","scene","news"])
    ]
    
    
    
    override func fetchData(){
        /// Sample code
        var tmpArr : [DUYTableViewCellViewModel] = []
        let section = self.getSectionViewModelAt(index: 0)
        for i in 0...dataCollection.count-1{
            let vm = ImageHorizontalListViewModel.init(sectionViewModel: section, tableViewModel: self, section: 0, row: i)
            vm.subTitle = dataCollection[i].1
            vm.mainTitle = dataCollection[i].0
            vm.listImage = dataCollection[i].2
            tmpArr.append(vm)
        }
        for i in 0...dataImageTitle.count-1 {
            let vm = ImageTitleCellViewModel.init(sectionViewModel: section, tableViewModel: self, section: 0, row : i)
            vm.subTitle = dataImageTitle[i].0
            vm.mainTitle = dataImageTitle[i].1
            vm.image = UIImage.init(named: dataImageTitle[i].2)
            tmpArr.append(vm)
        }
        
        for i in 0...dataBasic.count-1{
            let vm = DUYTableViewCellViewModel.init(sectionViewModel: section, tableViewModel: self, section: 0, row : i)
            vm.subTitle = dataBasic[i].0
            vm.mainTitle = dataBasic[i].1
            tmpArr.append(vm)
        }
        
        section.cellViewModelArray = tmpArr
        /// Sample code end
        
    }
}

Step 3:

You create your own ViewController with your own tableView, your ViewController should inherit DUYTableViewController, and call setUpTableView in viewDidLoad with your ViewModel in Step 2 and your tableView.

class TestTableViewController: DUYTableViewController {
    @IBOutlet weak var tableView : UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpTableView(tableViewModel: TestTableViewModel.init(), tableView: self.tableView)
        self.tableViewModel.fetchData()
    }
    //...Your code below
}

Step 4:

Register cells, events, and cell UI properties:

  override func getRegisteredCells() -> [TableCellRegistration<DUYTableviewCell>] {
        let results : [TableCellRegistration<DUYTableviewCell>] = [
            (classType : ImageTitleTableViewCell.self, isFromXib: true),
            (classType : ImageHorizontalListTableViewCell.self, isFromXib: true)
        ]
        return results
    }
    
    override func setUpBinding() {
        super.setUpBinding()
        self.tableViewModel.onDataChanged = {
            [weak self] vm, section in
            self?.tableView.reloadData()
        }
        
        self.tableViewModel.onRowTap = {
            [weak self] vm, section, row in
            guard let vm = vm as? DUYTableViewCellViewModel else {return}
            self?.showAlert(title: vm.mainTitle, message: vm.subTitle + "\(row)")
        }
    }
    
    override func configureCellViewModel(viewModel: DUYTableviewCellViewModelProtocol, section: Int, row: Int) {
        super.configureCellViewModel(viewModel: viewModel, section: section, row: row)
        switch viewModel {
        case let someVm as ImageTitleCellViewModel:
            let _ = someVm
                .setIdentifierCell(class: ImageTitleTableViewCell.self)
                .setCellHeight(h: 223)
        default:
            return
        }
    }

Step 5:

Create your cells needed for the tableView and let it inherit DUYTableViewCell, implement setData function

  override func setData(viewModel: DUYTableviewCellViewModelProtocol) {
        guard let viewModel = viewModel as? ImageTitleCellViewModel else {return}
        self.lblTitle.text = viewModel.mainTitle
        self.lblSubtitle.text = viewModel.subTitle
        self.imgView.image = viewModel.image
    }

Step 6:

Check your results, here is my sample result from the example project

Author

duycao2506, [email protected]

License

DUYTableViewController is available under the MIT license. See the LICENSE file for more info.

duytableviewcontroller's People

Contributors

duycao2506 avatar duyckl2506 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.