Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用segmentcontroller更改tableview_Ios_Swift_Uitableview_Cocoapods_Segment - Fatal编程技术网

Ios 使用segmentcontroller更改tableview

Ios 使用segmentcontroller更改tableview,ios,swift,uitableview,cocoapods,segment,Ios,Swift,Uitableview,Cocoapods,Segment,我想使用XMSegmentController()来更改不同的tableview,我不知道我的程序中缺少了什么,这会导致运行时出现黑色情况。有人知道要给我的程序添加什么吗?多谢各位 以下是机器翻译代码: import UIKit import XMSegmentedControl class ViewController: UIViewController, XMSegmentedControlDelegate,UITableViewDelegate,UITableViewDataSource

我想使用XMSegmentController()来更改不同的tableview,我不知道我的程序中缺少了什么,这会导致运行时出现黑色情况。有人知道要给我的程序添加什么吗?多谢各位

以下是机器翻译代码:

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)")

        }
    }
}

你的初始ViewController是什么?你能签入你的故事板吗

设置初始viewcontroller

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

你的初始ViewController是什么?你能签入你的故事板吗

设置初始viewcontroller

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

将控制器设置为初始ViewController以显示控制器。现在使用委托方法中的
selectedSegment
参数,并根据它使用
hide/show
表视图

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableview1.isHidden = selectedSegment != 0
        tableview2.isHidden = selectedSegment != 1
    }
}
也可以像这样使用单个
tableView
,而不是使用两个
tableView

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
}
xmSegmentedControl
的委托方法中,只需重新加载tableView即可

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableView.reloadData()
    }
}

将控制器设置为初始ViewController以显示控制器。现在使用委托方法中的
selectedSegment
参数,并根据它使用
hide/show
表视图

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableview1.isHidden = selectedSegment != 0
        tableview2.isHidden = selectedSegment != 1
    }
}
也可以像这样使用单个
tableView
,而不是使用两个
tableView

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
}
xmSegmentedControl
的委托方法中,只需重新加载tableView即可

func xmSegmentedControl(_ xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {
    if xmSegmentedControl == segmentedControl1 {
        tableView.reloadData()
    }
}

我的故事板中只有ViewController,初始ViewController是ViewControllerOpps,我忘了按你说的做,但是如何切换tableview?是的,我想你忘了添加初始ViewController。先添加一个,我正在编辑切换的答案。我的故事板中只有ViewController,初始ViewController是ViewControllerOpps,我忘了按你说的做,但是如何切换tableview?是的,我想你忘了添加初始viewcontroller。先这样做,我正在编辑切换的答案。谢谢你解决我的问题@廖豪豪 欢迎伴侣:)同时检查编辑后的答案并忽略多个TableView的使用我想尝试一下,等待你的回复,谢谢你谢谢你解决我的问题@廖豪豪 欢迎伴侣:)同时选中编辑的答案并忽略多个TableView的使用我想尝试,等待您回复,谢谢我将使用带有两个
UITableViewController
s的容器视图控制器作为其子对象。如果你想添加更多的片段,它会更加灵活。我会使用一个容器视图控制器,它有两个
UITableViewController
s作为它的子对象。它更灵活,以防您想要添加更多的分段。