廖豪豪 廖豪豪 -3 years ago 101
Swift Question

Use segmentcontroller to change tableview

I want to use XMSegmentController(https://cocoapods.org/?q=segmen) to change the different tableview,I do not know what is missing in my program, which leads to a black situation when I run. Does anyone know what to add to my program? Thank you.

Here is mt code:

import UIKit
import XMSegmentedControl

class ViewController: UIViewController, XMSegmentedControlDelegate,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var segmentedControl1: XMSegmentedControl!
@IBOutlet weak var tableview1: UITableView!
@IBOutlet weak var tableview2: UITableView!

let one = ["1","2","3"]
let two = ["4","5","6"]
override func viewDidLoad() {
super.viewDidLoad()

segmentedControl1.delegate = self
segmentedControl1.segmentTitle = ["One", "Two"]

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "left", for: indexPath)
cell.textLabel?.text = one[indexPath.row]
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "right", for: indexPath)
cell.textLabel?.text = two[indexPath.row]
return cell
}
}


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

if tableView == self.tableview1 {
return one.count
}
return two.count

}

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}


func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
if xmSegmentedControl == segmentedControl1 {
print("SegmentedControl1 Selected Segment: \(selectedSegment)")

}
}
}

Answer Source

Set your controller as initial ViewController to show your Controller. Now use selectedSegment argument from delegate method and hide/show the tableView according to it.

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableview1.isHidden = selectedSegment != 0
        tableview2.isHidden = selectedSegment != 1
    }
}

Also instead of using two tableView you can use single tableView like this.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if segmentedControl1.selectedSegment == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "left", for: indexPath)
        cell.textLabel?.text = one[indexPath.row]
        return cell
    }else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "right", for: indexPath)
        cell.textLabel?.text = two[indexPath.row]
        return cell
    }
}


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

    if segmentedControl1.selectedSegment == 0 {
        return one.count
    }
    return two.count

}

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

And in delegate method of xmSegmentedControl simply reload the tableView.

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableView.reloadData()
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download