通过更改分段控件来更改视图

时间:2017-07-08 05:32:31

标签: ios swift xcode uiview uisegmentedcontrol

我有一个视图UIViewController,它包含两个容器,一个容器嵌入到UIViewController,第二个嵌入到UITableViewController。 UIViewController有一个包含3个段的SegmentedControl。我想根据每个段更改UITableViewController内容。哪种方式更好实现?

其中一个表格视图的示例:(我还有两首歌曲和艺术家)

class AlbumsSegmentView: UITableViewController{

    let qryAlbums = MPMediaQuery.albums()
    var allAlbums : [MPMediaItem] = []
    var contentView = UIViewController()


    var myMP = MPMusicPlayerController.systemMusicPlayer()

    override func viewDidLoad() {

        /* Background */
        tableView.backgroundColor = UIColor.clear
        tableView.separatorColor = UIColor.clear

        /*  Grouping list  */
        qryAlbums.groupingType = MPMediaGrouping.album
        allAlbums += MPMediaQuery.albums().items!

        self.clearsSelectionOnViewWillAppear = false
        self.view.isHidden=true
    }



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

        let cell: CustomTableViewCellAlbum = CustomTableViewCellAlbum(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
        cell.imageView!.clipsToBounds = true


        /*  For Album list  */
         let currLoc = qryAlbums.collectionSections![indexPath.section].range.location
         let rowItem = qryAlbums.collections![indexPath.row + currLoc]


        //Main text is Album name
        cell.textLabel!.text = rowItem.items[0].albumTitle
        cell.textLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: 22)
        cell.textLabel!.textColor = UIColor.white


        // Detail text is Album artist
        cell.detailTextLabel!.text = rowItem.items[0].albumArtist!
        cell.detailTextLabel!.font = UIFont(name: "HelveticaNeue-Thin", size:18)
        cell.detailTextLabel!.textColor = UIColor.white


        cell.backgroundColor = UIColor.clear

        let seperatorImageView = UIImageView.init(image: UIImage.init(named: "Separator.png"))
        seperatorImageView.frame = CGRect(x: 10, y: cell.contentView.frame.size.height+26, width: cell.contentView.frame.size.width, height: 3)
        cell.contentView.addSubview(seperatorImageView)

        // Or number of songs from the current album if you prefer
        //cell.detailTextLabel!.text = String(rowItem.items.count) + " songs"

        // Add the album artwork
        let artWork = rowItem.representativeItem?.artwork
        let tableImageSize = CGSize(width: 10, height: 10) //doesn't matter - gets resized below
        let cellImg: UIImageView = UIImageView(frame: CGRect(x:12,y:6,width:60,height:60))
        cellImg.image = artWork?.image(at: tableImageSize)
        cell.addSubview(cellImg)

        return cell
    }

    /*  For Songs list  */

    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        let sectionIndexTitles = qryAlbums.itemSections!.map { $0.title }
        return sectionIndexTitles
    }
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return index
    }
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return (qryAlbums.itemSections![section].title)
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return allAlbums.count
        //return (qryAlbums.collections?.count)!;
        //return qryAlbums.collectionSections![section].range.length
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let currLoc = qryAlbums.collectionSections![indexPath.section].range.location
        myMP.setQueue(with: qryAlbums.collections![indexPath.row + currLoc])
        myMP.play()
    }

}

class CustomTableViewCellAlbum: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    // Here you can customize the appearance of your cell
    override func layoutSubviews() {
        super.layoutSubviews()
        // Customize imageView like you need
        self.imageView?.frame = CGRect(x:12,y:6,width:60,height:60)
        self.imageView?.contentMode = UIViewContentMode.scaleAspectFit

        self.textLabel?.frame = CGRect(x:78, y:5, width:200, height:30)
        self.detailTextLabel?.frame = CGRect(x:78, y:35, width:200, height:30)

    }
}

容器示例:

class MyParentViewController: UIViewController {

    var buttonPanelViewController: PanelViewController!
    var tableViewController: TableViewController2!


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if let tableViewController = segue.destination as? TableViewController {
            self.tableViewController = tableViewController
        }
        else if let buttonPanelViewController = segue.destination as? PanelViewController {
            self.buttonPanelViewController = buttonPanelViewController
        }
    }
}

class TableViewController: UITableViewController {}

class PanelViewController: UIViewController {}

故事板视图示例 Picture example

UPD:我尝试的最后一件事是在MyParentViewController中创建3个不同的UIViews并将它们附加到不同的类(歌曲,艺术家,专辑)并尝试在切换后逐个隐藏细分,但它不起作用。也许还有另一种更好的方法来实现它?

class MyParentViewController: UIViewController {

    var buttonPanelViewController: PanelViewController!


      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if let artistViewController = segue.destination as? ArtistViewController {
            self.artistViewController = artistViewController
        }
        else if let buttonPanelViewController = segue.destination as? PanelViewController {
            self.buttonPanelViewController = buttonPanelViewController
        }
        else if let songViewController = segue.destination as? SongViewController { self.songViewController = songViewController
        }
        else let albumViewController = segue.destination as? AlbumViewController { self.albumViewController = albumViewController
         }
        }
    }



}

class TableViewController: UITableViewController {}

class PanelViewController: UIViewController {

    @IBOutlet public weak var segmentTest: UISegmentedControl!


    @IBAction func testAction(_ sender: UISegmentedControl) {

        if (segmentTest.selectedSegmentIndex == 0){
            BackTableVC().view.isHidden=false
            SongsSegmentView().view.isHidden = true
            AlbumsSegmentView().view.isHidden = true
            print("Hi")

        }else if (segmentTest.selectedSegmentIndex == 1){
            BackTableVC().view.isHidden = true
            SongsSegmentView().view.isHidden = false
            AlbumsSegmentView().view.isHidden = true
            print("Thank")
        }else{
            BackTableVC().view.isHidden = true
            SongsSegmentView().view.isHidden = true
            AlbumsSegmentView().view.isHidden = false
            print("You")
        }

    }

     }

class ArtistViewController: UIView{

} class SongViewController: UIView{

} class AlbumViewController: UIView{

}

1 个答案:

答案 0 :(得分:0)

解决此问题的最佳方法是不更改每个分段控件的表视图,而是更改每个分段控件上的单元格。为表视图控制器注册3个单元格,一旦分段控制值发生变化,调用委托函数来更改哪个单元格出列,然后用新内容更新表格视图。

从BackPanelVC中,在已更改值的情况下,从分段控件到其类创建一个操作。在该操作中,调用一个委托函数,用它来切换到哪个控件。

protocol SegmentControlChangeDelegate {
    func segmentedControlChangedValue(_ value: Int)
}

class PanelViewController: UIViewController {

@IBOutlet public weak var segmentTest: UISegmentedControl!
var delegate: SegmentControlChangeDelegate?


@IBAction func testAction(_ sender: UISegmentedControl) {
    delegate.segmentedControlChangedValue(value: sender.selectedSegmentIndex)
    self.performSegue(with: "identifier")
}

}

并且在tableviewcontroller中符合委托,如:

class AlbumsSegmentView: UITableViewController, SegmentControlChangeDelegate {
     func segmentedControlChangedValue(_ value: Int){
     // Change which cell is being displayed with some type of boolean or switch statement that will be recalled in cell.dequeue
     }
}

并确保在tableviewcontroller中将委托设置为self

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationVC = segue.destination as? PanelViewController {
        destinationVC.delegate = self
    }
}