在tableView中选择多个项目

时间:2017-10-20 08:34:09

标签: ios swift uitableview

在我编写本课程的教程的帮助下

import UIKit
import AVFoundation

class SelectClass: UIViewController, UITableViewDelegate, 
UITableViewDataSource  {


    @IBOutlet weak var tableView: UITableView!

    var list : [QCategoryy] = [QCategoryy]()
    var audioPlayer : AVAudioPlayer!



    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        self.title = "Categories"
        list = NearbyPlaces.getCategories()
    }



    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


        list.sort() { $0.views > $1.views}
        tableView.reloadData()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    @IBAction func backTapp(_ sender: Any) {

        let audioUrl = NSURL.fileURL(withPath: Bundle.main.path(forResource: 
        "pop_drip", ofType: "m4a")!)
        do{
            try AVAudioSession.sharedInstance().setActive(true)
            try AVAudioSession.sharedInstance()
            .setCategory(AVAudioSessionCategoryPlayback)

            try audioPlayer = AVAudioPlayer(contentsOf: audioUrl)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        }
        catch _ as NSError
        {

        }
        dismiss(animated: true, completion: nil)

    }


    @IBAction func doneTapp(_ sender: Any) {

        let audioUrl = NSURL.fileURL(withPath: Bundle.main.path(forResource: 
        "pop_drip", ofType: "m4a")!)
        do{
            try AVAudioSession.sharedInstance().setActive(true)
            try AVAudioSession.sharedInstance()
            .setCategory(AVAudioSessionCategoryPlayback)

            try audioPlayer = AVAudioPlayer(contentsOf: audioUrl)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        }
        catch _ as NSError
        {

        }

}




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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
    IndexPath) -> UITableViewCell {
        let identifier = "CATEGORY_CELL"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, 
        for: indexPath)
        cell.textLabel?.text = list[indexPath.row].name
        return cell
    }

    let nearbySearchSegueIdentifier = "goToMcourse"
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
    IndexPath) {
        self.performSegue(withIdentifier: nearbySearchSegueIdentifier, 
        sender: list[indexPath.row])
    }














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

        if segue.identifier == nearbySearchSegueIdentifier {
            guard let category = sender as? QCategoryy else {
                return
            }
            if let vc = segue.destination as? CourseClass2 {
                vc.category = category
            }
        }
    }
}









extension QCategoryy {
    private static let ketPrefix = "category-"

    var views:Int {
        get {
            return UserDefaults.standard.integer(forKey: QCategoryy.ketPrefix + name)
        }
    }

    func markView() {
        UserDefaults.standard.set(views + 1, forKey: QCategoryy.ketPrefix + name)
    }
}

这是一个tableView,其中包含一个地点类型列表(google places),如果我点击其中一个,我将进入控制器,之后我会看到附近所有类型的地方。但是我希望可以选择在tableView上选择多行,也可以在按下这样的按钮后继续

 @IBAction func doneTapp(_ sender: Any) { 
} 

我如何改变这门课程以实现这一目标?

1 个答案:

答案 0 :(得分:0)

让我们回顾一下。

在这里您可以找到有关多种选择的绝佳答案: UITableView Multiple Selection

首先是: Service

等等。

  

现在我已经点击了多行,我怎样才能在按钮功能中使用这个self.performSegue(withIdentifier:nearbySearchSegueIdentifier,sender:list [indexPath.row]),

您不必向public class MyArray { public static void main(String args[]) { int ar[] = { 08, 047, 06, 05, 04, 03, 02, 091 }; } } 提供tableView.allowsMultipleSelection = true,也可以接受sender。只需指定segue id和nil作为发件人即可完成设置:

performSegue

但是,你现在应该准备这个segue。如果您关心发件人,请务必检查是否可以为空。

nil

所以你剩下的就是指明你想要传递的确切信息类型。在所需类型的视图控制器中声明公共字段,并根据所选索引传递所需数据,类似于:

@IBAction func doneTapp(_ sender: Any) { 
    self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: nil)
}