当我从tableView传递时,我的tableview数据在另一个视图控制器中不起作用。我可以通过print()在控制台中看到传递的数据,但是当我想使用它时它不起作用。
这是我的TableView
import UIKit
class BroadcastList: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
@IBOutlet var slider: UIBarButtonItem!
var articles: [Article]? = []
override func viewDidLoad() {
super.viewDidLoad()
if(revealViewController() != nil)
{
slider.target = self.revealViewController()
slider.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
self.view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
}
fetchArticles()
}
func fetchArticles(){
let urlRequest = URLRequest(url: URL(string: "http://myurl.com")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error)
return
}
print(response)
self.articles = [Article]()
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let articlesFromJson = json["UserList"] as? [[String : AnyObject]] {
for articleFromJson in articlesFromJson {
let article = Article()
if let title = articleFromJson["name"] as? String, let author = articleFromJson["userid"] as? String, let desc = articleFromJson["startedt"] as? String, let url = articleFromJson["liveURL"] as? String, let urlToImage = articleFromJson["image"] as? String, let streamID = articleFromJson["streamID"] as? String {
article.author = author
article.desc = desc
article.headline = title
article.url = url
article.streamID = streamID
article.imageUrl = urlToImage
//print(url)
}
self.articles?.append(article)
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
} catch let error {
print(error)
}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as! ArticleCell
cell.title.text = self.articles?[indexPath.item].headline
cell.desc.text = self.articles?[indexPath.item].desc
cell.author.text = self.articles?[indexPath.item].author
cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageUrl!)!)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.articles?.count ?? 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let DestViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "watchLive") as! WatchLiveVideo
let strmid = (self.articles?[indexPath.item].streamID)!
let urlc = (self.articles?[indexPath.item].url)!
print(urlc)
DestViewController.getStreamID = strmid
DestViewController.vgetURL = urlc
self.present(DestViewController, animated: true, completion: nil)
}
}
extension UIImageView {
func downloadImage(from url: String){
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error)
return
}
DispatchQueue.main.async {
self.layer.cornerRadius = self.frame.size.width/2
self.clipsToBounds = true
self.image = UIImage(data: data!)
}
}
task.resume()
}
func downloadImageSquare(from url: String){
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error)
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
我的另一个ViewController代码就在这里。
import UIKit
import AVKit
import AVFoundation
//import MediaPlayer
class WatchLiveVideo: UIViewController {
var getStreamID = ""
var vgetURL = ""
var playerLayer: AVPlayerLayer?
var player: AVPlayer?
var team1S = ""
var team2S = ""
//var moviePlayer : MPMoviePlayerController!
@IBOutlet var playerholder: UIView!
@IBOutlet var vPlayer: UIView!
@IBOutlet var teamA: UILabel!
@IBOutlet var teamB: UILabel!
@IBOutlet var team2: UILabel!
@IBOutlet var team1: UILabel!
var count: Int = 1;
override func viewDidLoad() {
super.viewDidLoad()
self.playerholder.layer.zPosition = 1;
team1.text = "0"
team2.text = "0"
// Do any additional setup after loading the view, typically from a nib.
_ = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(WatchLiveVideo.self.getScore), userInfo: nil, repeats: true)
// Do any additional setup after loading the view.
//let videoURL = NSURL(string: "http://35.160.212.231:1935/live/shopno/playlist.m3u8")
let videoURL = NSURL(string: vgetURL)
print(videoURL)
let player = AVPlayer(url: videoURL as! URL)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
vPlayer.addSubview(playerController.view)
playerController.view.frame = vPlayer.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getScore() {
let myUrl = URL(string: "http://myurl.com");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
//input data bind to send server
//let streamID = "88"
let streamID = self.getStreamID
let postString = "streamID=\(streamID)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let title = parseJSON["team1"] as? String
let title2 = parseJSON["team2"] as? String
DispatchQueue.main.async {
self.team1.text = title
self.team2.text = title2
}
}
} catch {
print(error.localizedDescription)
}
}
task.resume()
}
}