beginner swift coder.
I created a table view of basketball teams on my view controller. After that, I created a nav controller for the view controller. I entered 7 basketball teams to my table view. Now, I have created a second view controller, so that when I tap on any of the 7 basketball teams, it will open another window.
My question is, how do I display different information when tapping on different basketball teams?
enter image description here Main View controller
import UIKit import AVFoundation
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var nbaMusic: AVAudioPlayer = AVAudioPlayer()
let nbateams = ["Knicks", "Cavaliers", "Thunder", "Warriors", "Lakers", "Bulls", "Magic"]
@IBOutlet var myTableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nbateams.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell:UITableViewCell = myTableView.dequeueReusableCellWithIdentifier("nba", forIndexPath: indexPath)
myCell.textLabel?.text = nbateams[indexPath.row]
myCell.imageView?.image = UIImage(named: nbateams[indexPath.row])
return myCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let data = nbateams[indexPath.row]
let playerView = playerViewController()
detailView.dicData = data
self.navigationController?.pushViewController(playerView, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTableView.dataSource = self
myTableView.delegate = self
let nbaMusicURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Roundball_Rock", ofType: "mp3")!)
do{
nbaMusic = try AVAudioPlayer(contentsOfURL: nbaMusicURL, fileTypeHint: nil)
}
catch _ {
return print("File not found")
}
nbaMusic.numberOfLoops = 0
nbaMusic.prepareToPlay()
nbaMusic.play()
class playerViewController: UIViewController {
let knicks = ["Carmelo.png"]
let Cavaliers = ["Lebron.png"]
@IBOutlet var playerInfoView: UILabel!
@IBOutlet var playerImage: UIImageView!
var imageView: String?
var dicData: String?
override func viewDidLoad() {
super.viewDidLoad()
playerImage.image = UIImage(named: dicData!)
// Do any additional setup after loading the view.
}
In MainViewController: viewDidLoad you set more:
myTableView.delegate = self
and implement:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let data = nbateams[indexPath.row]
let playerView = playerViewController() // or load from storyboad
playerView.dicData = data
self.navigationController?.pushViewController(playerView, animated: true)
}
in playerViewController create a data object, and transfer data(info need display) to it
@IBOutlet var playerInfoView: UILabel!
@IBOutlet var playerImage: UIImageView!
var imageView: String?
var dicData: String!
override func viewDidLoad() {
super.viewDidLoad()
playerImage.image = UIImage(named: dicData) // ex
// Do any additional setup after loading the view.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.