如何在Xcode和swift中划分或显示/推送新视图

时间:2018-02-23 18:57:30

标签: swift xcode uitableview uitabbarcontroller uistoryboardsegue

我有一个表格视图单元格,该单元格位于表格视图中,该表格视图位于ViewController中,该视图控制器位于页面视图控制器中,最后该页面视图控制器是选项卡式视图控制器中的选项卡。对于表视图中的每个单元格,我都有一个按钮,我想在按下该按钮时显示一个ViewController(以编程方式)我希望新的视图控制器不在标签栏控制器内。我该怎么做

这是故事板的各个层面 按钮 - >表格视图单元格 - >表格视图 - > ViewController - >页面视图控制器 - >标签栏控制器

这是我的tableviewcontroller

import UIKit

class TimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var backgroundImageView: UIImageView!
var cellTypes = [1,2,3,1,2,3,1,2,3,1,2,3,1,2,3]

override func viewDidLoad() {
    super.viewDidLoad()
    setupBackground(backgroundImage: #imageLiteral(resourceName: "Placeholder 1.jpeg"))
    setViewToProfileViewController()
}

func setupBackground(backgroundImage: UIImage) {
    backgroundImageView.image = backgroundImage

    // Background Image Blur Effect:
    let backgroundImageBlurEffect = UIBlurEffect(style: .light)
    let backgroundImageBlurEffectView = UIVisualEffectView(effect: backgroundImageBlurEffect)
    backgroundImageBlurEffectView.frame = backgroundImageView.frame
    backgroundImageView.addSubview(backgroundImageBlurEffectView)
}

// Change View Controller To Profle View Controller
func setViewToProfileViewController() {
    print("H")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
    self.navigationController?.pushViewController(vc, animated: true)
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell", for: indexPath) as! TimelineCell

    // Decide Which Cell To Create
    switch cellTypes[indexPath.row] {
    case 1:
        cell = cell.createPartyCell(cellToUse: cell)
        tableView.rowHeight = 220
    case 2:
        cell = cell.createPostCell(cellToUse: cell)
        tableView.rowHeight = 195
    case 3:
        cell = cell.createFriendRequestCell(cellToUse: cell)
        tableView.rowHeight = 120
    default:
        break
    }

    return cell
}

}

这是我的tableviewcellClass

import UIKit

class TimelineCell: UITableViewCell {

let timelineViewController = TimelineViewController()

//Initialize Storyboard Elements
@IBOutlet weak var timelineCellView: UIView!

// Initialize Properties
let cellTitle = UILabel()
let cellBackgroundImageView = UIImageView()
let cellBackgroundImageBlurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .prominent))
let profileObject = ProfileObject(image: #imageLiteral(resourceName: "Placeholder 1.jpeg"), width: 32, height: 32)
let cellDateLabel = UILabel()
let cellDescriptionBackground = UILabel()
let cellDescription = UITextView()
let cellImageView = UIImageView()
let cellNameLabel = UILabel()


// Prepare For Cell Reuse
override func prepareForReuse() {
    // Recycle Cell Subviews
    cellTitle.removeFromSuperview()
    cellBackgroundImageView.removeFromSuperview()
    cellBackgroundImageBlurEffectView.removeFromSuperview()
    cellDateLabel.removeFromSuperview()
    cellDescriptionBackground.removeFromSuperview()
    cellDescription.removeFromSuperview()
    cellImageView.removeFromSuperview()
    cellNameLabel.removeFromSuperview()
}

@objc func profileObjectClick(){
    //THIS IS WHERE I WANT TO PERFORME THE SEGUE
}


 // - - - - - CREATE - - - - -
// - - - - - CELLS - - - - -

// CREATE PARTY CELL
func createPartyCell(cellToUse postCell: TimelineCell) -> TimelineCell {

    // Initialize Party Cell View
    timelineCellView.layer.masksToBounds = true
    timelineCellView.layer.cornerRadius = 5

    // Initialize Party Cell Background Image
    cellBackgroundImageView.frame = CGRect(x: 0, y: 0, width: timelineCellView.frame.width, height: 200)
    cellBackgroundImageView.image = #imageLiteral(resourceName: "Placeholder 1.jpeg")
    cellBackgroundImageBlurEffectView.frame = cellBackgroundImageView.frame
    cellBackgroundImageView.addSubview(cellBackgroundImageBlurEffectView)

    // Initialize Profile Object
    let partyCellProfileObject = profileObject.createProfileObjectView()
    profileObject.profileObjectButton.addTarget(self, action: #selector(profileObjectClick), for: .touchUpInside)
    partyCellProfileObject.frame.origin.x = 10
    partyCellProfileObject.frame.origin.y = 10

    // Initialize Party Cell Title Label
    cellTitle.frame = CGRect(x: 52, y: 10, width: timelineCellView.frame.width - 62 , height: 32)
    cellTitle.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    cellTitle.font = UIFont(name: "Roboto-Regular", size: 15)
    cellTitle.text = "  My 16th Birthday"
    cellTitle.layer.masksToBounds = true
    cellTitle.layer.cornerRadius = 5

    // Initialize Party Cell Date Label
    cellDateLabel.frame = CGRect(x: timelineCellView.frame.width / 2, y: 10, width: timelineCellView.frame.width / 2 - 15, height: 32)
    cellDateLabel.font = UIFont(name: "Roboto-Thin", size: 12)
    cellDateLabel.text =  "6hr ago"
    cellDateLabel.textAlignment = .right

    // Cell Description Background
    cellDescriptionBackground.frame = CGRect(x: 10, y: 52, width: timelineCellView.frame.width - 20, height: cellBackgroundImageView.frame.height - 62)
    cellDescriptionBackground.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    cellDescriptionBackground.layer.masksToBounds = true
    cellDescriptionBackground.layer.cornerRadius = 5

    // Cell Description
    cellDescription.frame = CGRect(x:  15, y: 52, width: timelineCellView.frame.width - 130, height: cellDescriptionBackground.frame.height)
    cellDescription.isScrollEnabled = false
    cellDescription.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at maximus nulla, a sollicitudin nunc. Vestibulum a tempus tortor. Phasellus dictum bibendum urna ac hendrerit. Nullam aliquet turpis et tellus iaculis, id volutpat metus sagittis. Vivamus ultrices libero quis risus blandit, eget convallis mi aliquam. Sed sed mi sit amet tellus posuere mattis vitae ac eros. Ut in placerat turpis. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed condimentum at purus et rhoncus. Phasellus feugiat hendrerit hendrerit. Etiam in eleifend nisi, sit amet rhoncus sem. Sed convallis quis est id sagittis. Maecenas sodales justo nec purus rutrum, vel scelerisque odio sagittis. Nullam feugiat ac risus sit amet iaculis. Proin et libero eleifend, cursus orci ac, accumsan quam. Maecenas eget diam velit. Curabitur vehicula ornare felis a commodo. Cras enim est, feugiat id posuere eget, tempor a nisi. In lobortis et dui ultricies laoreet. Ut nibh magna, interdum vitae pellentesque tempor, ornare et sapien. Duis nec libero nec ante hendrerit dapibus. Aenean pulvinar tincidunt leo a maximus. Proin fringilla nibh posuere commodo feugiat."
    cellDescription.isEditable = false
    cellDescription.font = UIFont(name: "Roboto-Regular", size: 13)

    // Cell Image
    cellImageView.image = #imageLiteral(resourceName: "Placeholder 1.jpeg")
    cellImageView.frame = CGRect(x: cellDescriptionBackground.frame.width - 100, y: 62, width: 100, height: cellDescriptionBackground.frame.height - 20)
    cellImageView.contentMode = .scaleAspectFill
    cellImageView.layer.masksToBounds = true
    cellImageView.layer.cornerRadius = 5

    // Add Party Cell Subviews
    timelineCellView.addSubview(cellBackgroundImageView)
    timelineCellView.addSubview(cellTitle)
    timelineCellView.addSubview(cellDateLabel)
    timelineCellView.addSubview(cellDescriptionBackground)
    timelineCellView.addSubview(cellDescription)
    timelineCellView.addSubview(cellImageView)
    timelineCellView.addSubview(partyCellProfileObject)
    return postCell
}

// CREATE POST CELL
func createPostCell(cellToUse postCell: TimelineCell) -> TimelineCell {

    // Initialize Post Cell View
    timelineCellView.layer.masksToBounds = true
    timelineCellView.layer.cornerRadius = 5

    // Initialize Post Cell Background Image
    cellBackgroundImageView.frame = CGRect(x: 0, y: 0, width: timelineCellView.frame.width, height: 175)
    cellBackgroundImageView.image = #imageLiteral(resourceName: "Placeholder 1.jpeg")
    cellBackgroundImageView.contentMode = .scaleAspectFill

    // Bottom Blurr Effect
    cellBackgroundImageBlurEffectView.frame = CGRect(x: 0, y: 125, width: timelineCellView.frame.width, height: 50)
    cellBackgroundImageView.addSubview(cellBackgroundImageBlurEffectView)

    // Initialize Profile Object
    let postCellProfileObject = profileObject.createProfileObjectView()
    postCellProfileObject.frame.origin.x = 10
    postCellProfileObject.frame.origin.y = 134

    // Initialize Party Cell Title Label
    cellTitle.frame = CGRect(x: 52, y: 125, width: 200, height: 40)
    cellTitle.font = UIFont(name: "Roboto-Regular", size: 15)
    cellTitle.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 0)
    cellTitle.text = "Just Some Picks"

    // Name Label
    cellNameLabel.frame = CGRect(x: 52, y: 150, width: 200, height: 20)
    cellNameLabel.text = "Zachary Gameiro"
    cellNameLabel.font = UIFont(name: "Roboto-Thin", size: 10)
    cellNameLabel.textAlignment = .left
    cellNameLabel.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 0)

    // Initialize Post Cell Date Label
    cellDateLabel.frame = CGRect(x: timelineCellView.frame.width / 2, y: 125, width: timelineCellView.frame.width / 2 - 15, height: 50)
    cellDateLabel.font = UIFont(name: "Roboto-Thin", size: 12)
    cellDateLabel.text =  "5hr ago"
    cellDateLabel.textAlignment = .right


    // Add Post Cell Subviews
    timelineCellView.addSubview(cellBackgroundImageView)
    timelineCellView.addSubview(cellTitle)
    timelineCellView.addSubview(cellDateLabel)
    timelineCellView.addSubview(cellNameLabel)
    timelineCellView.addSubview(postCellProfileObject)
    return postCell
}

 // CREATE FRIEND REQUEST CELL
func createFriendRequestCell(cellToUse friendRequestCell: TimelineCell) -> TimelineCell {

    // Initialize Friend Request Cell View
    timelineCellView.layer.masksToBounds = true
    timelineCellView.layer.cornerRadius = 5

    // Initialize Friend Request Cell Background
    cellBackgroundImageView.frame = CGRect(x: 0, y: 0, width: timelineCellView.frame.width, height: 100)
    cellBackgroundImageView.image = #imageLiteral(resourceName: "Placeholder 1.jpeg")
    cellBackgroundImageView.contentMode = .scaleAspectFill
    cellBackgroundImageBlurEffectView.frame = cellBackgroundImageView.frame
    cellBackgroundImageView.addSubview(cellBackgroundImageBlurEffectView)

    // Initialize Profile Object
    let friendRequestCellProfileObject = profileObject.createProfileObjectView()
    friendRequestCellProfileObject.frame.origin.x = 10
    friendRequestCellProfileObject.frame.origin.y = 10

    // Initialize Friend Request Cell Title Label
    cellTitle.frame = CGRect(x: 52, y: 10, width: timelineCellView.frame.width - 62 , height: 32)
    cellTitle.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    cellTitle.font = UIFont(name: "Roboto-Regular", size: 15)
    cellTitle.text = "  Friend Request"
    cellTitle.layer.masksToBounds = true
    cellTitle.layer.cornerRadius = 5

    // Initialize Friend Request Cell Description
    cellNameLabel.frame = CGRect(x: 10, y: 52, width: timelineCellView.frame.width - 20 , height: 38)
    cellNameLabel.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    cellNameLabel.font = UIFont(name: "Roboto-Thin", size: 15)
    cellNameLabel.text = "David Has Requested To Be Friends"
    cellNameLabel.textAlignment = .center
    cellNameLabel.layer.masksToBounds = true
    cellNameLabel.layer.cornerRadius = 5

    // Add Friend Request Cell Subviews
    timelineCellView.addSubview(cellBackgroundImageView)
    timelineCellView.addSubview(cellTitle)
    timelineCellView.addSubview(friendRequestCellProfileObject)
    timelineCellView.addSubview(cellNameLabel)
    return friendRequestCell
}

}

0 个答案:

没有答案