Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How better implement UITableView with two "tabs"?

Both tables must be in one view controller, switching by segment control for example. Data is infinite, fetched from core data. I see three solutions:

1) create two objects of UITableView and filling them in shared datasource functions; keeping one of tableviews hidden

2) create two view controller containers, embed in main view controller, and perform completely separated data source methods; keeping one of containers hidden

3) use only one table view object, reloading data when needed, providing offset saving

Need your opinions. Which solution will be faster and/or more readable and right?

Update

Let me try to implement third option:

var tableViewOffsets = [Int: CGPoint]()

func segmentValueChanged(sender: UISegmentedControl) {
    tableViewOffsets[tableView.tag] = tableView.contentOffset
    tableView.tag = sender.selectedSegmentIndex
    tableView.reloadData()
    if let savedOffset = tableViewOffsets[tableView.tag] {
        tableView.setContentOffset(savedOffset, animated: false)
    }
}

func tableView_dataSourceMethodsTemplate(tableView: UITableView, ...) {
    if tableView.tag == 0 {
        //perform data source code for first tab
    } else {
        //perform data source code for second tab
    }
}
like image 559
Shadow Of Avatar asked Jul 25 '16 07:07

Shadow Of


1 Answers

option number third is easy and good memory optimize also, in my suggestion use tag Concept with single tableView if you selected the any segment assign the tag for your tableview (e.g yourtableview.tag = 1) and at the same time change the frame what ever you need,

Updated

func segmentValueChanged(sender: UISegmentedControl) {
tableViewOffsets[tableView.tag] = tableView.contentOffset
tableView.tag = sender.selectedSegmentIndex
var tablearray = [string]()
 if sender.selectedSegmentIndex ==  0
{
  // here fecth the value in coredata and append in array
 tablearray.append (yourdta)
 }else
  {
  // here fecth the value in coredata and append in array
    tablearray.append (yourdta)
  }


if let savedOffset = tableViewOffsets[tableView.tag] {
    tableView.setContentOffset(savedOffset, animated: false)
}
tableView.reloadData()
}

func tableView_dataSourceMethodsTemplate(tableView: UITableView, ...)    {
 return tablearray.count
}
like image 150
Anbu.Karthik Avatar answered Sep 27 '22 17:09

Anbu.Karthik