如何使用swift 3在tableview中传递数据?

时间:2018-02-23 16:43:02

标签: ios swift uitableview

我想得到,存储&将tableview中的URL传递给swift 3中的另一个表视图?

我很努力,但我不能这样做?请帮帮我。

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

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
}

}

类PlayView {

 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;

}

我希望在用户点击时从tableview获取网址。

我想在点击Tableview时获取一个URL,然后保存并将其传递给另一个tableview。

我可以在JAVA中执行此操作,但我无法在SWIFT 3中进行转换

这是我的java示例

{{1}}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

您需要实施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?

 }