-2

I have a requirement to design a screen as mentioned in the screenshot:

enter image description here Here I was requested to add 'N' number of UITableView dynamically as per the number of records available in database.

I searched a lot in google but there is no tutorial/guidance available. So I think this question will help lot of people who are all facing the same issue like me.

Saranya
  • 163
  • 2
  • 10
  • Are you sure it's "n tableviews" and not "n tableviewrows"? – Flavio Silverio May 31 '16 at 08:10
  • Yes N tableviews and not N tableviewrows. Please see the screenshot I have attached. – Saranya May 31 '16 at 08:14
  • 1
    So simple,, add a uiscrollview and then add uitableviews as you want with tag of each tableview. – aBilal17 May 31 '16 at 08:15
  • @Saranya it's better to add your picture here , the link in future will be unavailable so your question will become incomprehensible – Alessandro Ornano May 31 '16 at 08:16
  • But number of UITableView can be decided only during the runtime and not during the design time. Thats the real chellange here. – Saranya May 31 '16 at 08:16
  • You can achieve that with a UIScrollView with horizontal scroll, btw the question is too broad, you need to show some code in order to get help. Here you can find an answer about using multiple tableViews in the same ViewController: http://stackoverflow.com/questions/16195660/multiple-uitableview-in-single-viewcontroller – LS_ May 31 '16 at 08:17
  • @Saranya use paging view https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html – Yury May 31 '16 at 08:17
  • @AlessandroOrnano As I am new to this community I don't have enough reputation to embed image. – Saranya May 31 '16 at 08:18
  • @Signo In the link you provided, it was discussed about adding delegates to all the 3 UITableView but here my problem is how to add UITableview dynamically inside scrollview? – Saranya May 31 '16 at 08:22
  • In Android, we can achieve this using layout inflate technique. We can inflate the template layout and can add the inflated layout to our uiscrollview. Is there any alternate in ios with swift language? – Saranya May 31 '16 at 08:29
  • See this link http://stackoverflow.com/questions/16195660/multiple-uitableview-in-single-viewcontroller hope its helpful – Iyyappan Ravi May 31 '16 at 08:36
  • See answer of @Lion. is correct. – Pramod Tapaniya May 31 '16 at 08:38

2 Answers2

1

If you project must handle few tableViews you can realize it exactly like this project https://github.com/gabrieltheodoropoulos/iOS-Swift-PageControl but , instead of using a UIView by using a UITableView and you can do exactly what you want..

You can customize your main viewController to add name, age and the update button by correct the scrollView frame in your storyboard, it's simple to do.

P.S. About the memory consumption, if your project is to heavy, you can think about a method to reuse the tableView variables as explained better in this stack overflow post

Here below an example how to modify all code to realize it:

class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!

    let totalPages = 8  // here you put your datasource array count

    var currentTableView : UITableView!

    let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor()]

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        configureScrollView()
        configurePageControl()
    }

    func configureScrollView() {

        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height)
        scrollView.delegate = self

        // Load the TestView view from the TestView.xib file and configure it properly.
        for i in 0 ..< totalPages {
            // Load the MyTable : a XIB contain a tableView
            let myTable = NSBundle.mainBundle().loadNibNamed("MyTable", owner: self, options: nil)[0] as! UITableView

            // Set its frame and the background color.
            myTable.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
            myTable.backgroundColor = sampleBGColors[i]

            myTable.delegate = self
            myTable.dataSource = self
            self.currentTableView = myTable

            let label = myTable.viewWithTag(1) as! UILabel
            label.text = "Page #\(i + 1)"

            scrollView.addSubview(myTable)
        }
    }


    func configurePageControl() {
        pageControl.numberOfPages = totalPages
        pageControl.currentPage = 0
    }


    // MARK: UIScrollViewDelegate method implementation

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // Calculate the new page index depending on the content offset.
        let currentPage = floor(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width);

        // Set the new page index to the page control.
        pageControl.currentPage = Int(currentPage)
    }


    // MARK: IBAction method implementation

    @IBAction func changePage(sender: AnyObject) {
        // Calculate the frame that should scroll to based on the page control current page.
        var newFrame = scrollView.frame
        newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage)
        scrollView.scrollRectToVisible(newFrame, animated: true)

    }

    // Add here UITableViewDelegate and UITableViewDatasource methods..
}
Community
  • 1
  • 1
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • Thanks for the great solution. I will try this approach. – Saranya May 31 '16 at 13:47
  • I have tried your approach and it is awesome. The problem is adding tableview in xib. Because I need checkbox and label in each cell. But I don't have prototype cell here. Is there another way to achieve this? – Saranya Jun 01 '16 at 07:00
  • Sure, you must create another XIB with UITableViewCell type. In this stackoverflow post you can find the solution: http://stackoverflow.com/a/25545185/1894067 – Alessandro Ornano Jun 01 '16 at 07:58
  • I have created one more XIB for UITableViewCell with M13Checkbox and label. But If I click the checkbox then it is not selected instead entire row is selected. Anything wrong here? – Saranya Jun 01 '16 at 21:30
  • I dont know M13Checkbox, try to open a new question , write the library link and post here the question link, I'll try to help you.. – Alessandro Ornano Jun 01 '16 at 21:34
  • In ios we dont have checkbox it seems. So the M13Checkbox is the library to achieve this. And I solved that issue in 2 steps. Step-1: Removed the selection colour for tableviewcell Step-2: On select and deselect of uitableviewcell, I set the checkbox state to checked or unchecked. – Saranya Jun 02 '16 at 14:28
0

As some comments says, you can use scroll view with horizontal scrolling and dynamically add tableviews but it can create large memory consumption if too many tableviews are there because scroll view keeps all tables in memory. Same thing may happen for paging also.

You need to use something which can be reuse if you required too many number of tables to show.

So, I think UICollectionView is better option!

Take collection view with one cell and show tablview in cell of collection view.

You can google for that how to use tableview in collectionview and you will got many solutions.

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75