How to show custom nested lists in iOS

2019-08-28 22:22发布

问题:

I want to show a list in iOS that itself will contain a list. For example I want to create a day wise task list in which the day and task are dynamic.So outer list is DAY LIST and inner list would be TASK LIST.Also want to handle the click on TASKS. How can I achieve this in iOS, have tried using Table Views but cannot achieve it since I want to show nested list.In short I want to show a list of list in iOS.

回答1:

    //
    //  ViewController.swift
    //  Ishika
    //
    //  Created by IShika on 12/06/17.
    //  Copyright © 2017 Ishika. All rights reserved.
    //

    import UIKit

    class ViewController: UIViewController {
        @IBOutlet weak var viewForHeader: UIView!

        @IBOutlet weak var tblViewForHome: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }



    }
    extension ViewController: UITableViewDataSource,UITableViewDelegate{

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            var cell = UITableViewCell()

                cell = tblViewForHome.dequeueReusableCell(withIdentifier: "FeaturedLocationsCell") as! FeaturedLocationCellClass

                   return cell
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

            //It should be greater than your collectionViewCell's size
            return 300.0
        }
    }

    //MARK:- UITABLEVIEWCELL CLASS


    class FeaturedLocationCellClass : UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
        @IBOutlet weak var colVForFeaturedLocations: UICollectionView!

        override func awakeFromNib() {

            super.awakeFromNib()
            colVForFeaturedLocations.dataSource = self
            colVForFeaturedLocations.delegate = self


        }
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 2
        }
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = colVForFeaturedLocations.dequeueReusableCell(withReuseIdentifier: "myFeautredLocationColVCell", for: indexPath) as! MyFeaturedLocationCollCellClass
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{

            return CGSize(width: screenWidth/2 + 50 , height: screenWidth/2 + 50)
        }



    }


    //MARK:- UICOLLECTIONVIEWCELL CLASS
    class MyFeaturedLocationCollCellClass: UICollectionViewCell{
    //Can draw outlets for your collectionView elements


        override func awakeFromNib() {
            super.awakeFromNib()

        }
    }