簡體   English   中英

如何使用swift 3在tableview中傳遞數據?

[英]How to pass data in tableview using swift 3?

我想在 swift 3 中獲取、存儲和傳遞 tableview 中的 URL 到另一個 table view?

我嘗試了很多,但我做不到? 請幫我。

class EpisodesTableViewController: UITableViewController { var episodes = Episode var audioPlayer : AVAudioPlayer!

var selectedVideoIndex: Int!


override func viewDidLoad()
{
    super.viewDidLoad()

    episodes = Episode.downloadAllEpisodes()
    self.tableView.reloadData()

    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.separatorStyle = .none

}

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int
{
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return episodes.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Episode Cell", for: indexPath) as! EpisodeTableViewCell
    let episode = episodes[indexPath.row]

    cell.episode = episode

    return cell
}

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
    IndexPath)
{
    tableView.deselectRow(at: indexPath, animated: true)

    performSegue(withIdentifier: "secondView", sender: indexPath.row)

}



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

    var next = segue.PViewController  as! PlayTableViewController

    next.index = sender as? Int
}

這是另一個代碼

class PlayTableViewController: UITableViewController

{

var play = [PlayView]()
var audioPlayer : AVAudioPlayer!

var indexOfCell:Int?


override func viewDidLoad() {
    super.viewDidLoad()

    super.viewDidLoad()

    play = PlayView.downloadAllEpisodes()
    self.tableView.reloadData()

    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.separatorStyle = .none
}

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

override func numberOfSections(in tableView: UITableView) -> Int
{
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return play.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Player Cell", for: indexPath) as! PlayerTableViewCell
    let playV = play[indexPath.row]

    cell.PV = playV

    return cell
}


// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
    IndexPath)
{
    tableView.deselectRow(at: indexPath, animated: true)

    let episode = play[indexPath.row]
    let player = AVPlayer(url: episode.url!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }
}

}

類播放視圖{

var name: String?
var thumbnailURL: URL?
var url: URL?

init(name: String, thumbnailURL: URL, url: URL)
{
    self.name = name
    self.thumbnailURL = thumbnailURL
    self.url = url
}

init(pvDictionary: [String : Any]) {
    self.name = pvDictionary["name"] as? String
    // url, createdAt, author, thumbnailURL
    url = URL(string: pvDictionary["alt_url"] as! String)
    thumbnailURL = URL(string: pvDictionary["alt_image"] as! String)
}

static func downloadAllEpisodes() -> [PlayView]
{
    var playView = [PlayView]()



    let url2 = URL(string: "http://nix2.iotabdapps.com/apk/items.json")
    let jsonData = try? Data(contentsOf: url2!)

    if let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {
        let pvDictionaries = jsonDictionary["items"] as! [[String : Any]]
        for pvDictionary in pvDictionaries {
            let newPlayView = PlayView(pvDictionary: pvDictionary)
            playView.append(newPlayView)
        }
    }

    return playView
}

}

我想在用戶單擊時從 tableview 獲取 URL。

我想在單擊 Tableview 時獲取一個 URL,然后將其保存並將其傳遞給另一個 tableview。

我可以在 JAVA 中執行此操作,但無法在 SWIFT 3 中進行轉換

這是我的 Java 示例

 itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            resultp = data.get(position);
            Intent intent = new Intent(context, FragmentDemoActivity.class);
            intent.putExtra("videoId", resultp.get(Main.VIDEO_ID));
            context.startActivity(intent);
            // Get the position



        }
    });
    return itemView;

有人可以幫我嗎?

你需要實現prepareForSegue方法

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
    IndexPath)
   { 
       tableView.deselectRow(at: indexPath, animated: true)

       performSegue(withIdentifier: "secondView", sender: indexPath.row)

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

    var next = segue.destinationViewController as! PlayTableViewController

    next.indexOfCell = sender as? Int

 }

//

 class PlayTableViewController:UITableViewController
 {
       var indexOfCell:Int?

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM