我创建了这个viewController:
import UIKit
import AVFoundation
class SelectClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var list : [QCategoryy] = [QCategoryy]()
var audioPlayer : AVAudioPlayer!
var limit = 0
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.allowsMultipleSelection = true
self.title = "Categories"
list = NearbyPlaces.getCategories()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
list.sort() { $0.views > $1.views}
tableView.reloadData()
}
@IBAction func doneTapp(_ sender: Any) {
self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: nil)
}
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)
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
/* cell.accessoryType = rowIsSelected ? .checkmark : .none */
cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
cell.textLabel?.text = list[indexPath.row].name
return cell
}
let nearbySearchSegueIdentifier = "goToMcourse"
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = .checkmark
/* self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: list[indexPath.row]) */
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = .none
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let sr = tableView.indexPathsForSelectedRows {
if sr.count == limit {
let alertController = UIAlertController(title: "Oops", message:
"You are limited to \(limit) selections", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in }))
self.present(alertController, animated: true, completion: nil)
return nil
}
}
return indexPath
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == nearbySearchSegueIdentifier {
guard let category = sender as? QCategoryy else {
return
}
if let selectedRows = tableView.indexPathsForSelectedRows {
if let vc : CourseClass2 = 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,我可以选择一行并转到下一个VC
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = .checkmark
/* self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: list[indexPath.row]) */
}
使用此performSegue
/* self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: list[indexPath.row]) */
取决于之前按过的行,结果不同。现在我说你可以选择多个单元格然后继续在下一个VC中有一个按钮
@IBAction func doneTapp(_ sender: Any)
self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: nil)
}
要推,但只是不知道为什么,不考虑选定的行,所以当我点击按钮时它会将我发送到下一页,好像我没有选择任何行。问题的原因是什么?我该如何解决?
答案 0 :(得分:0)
似乎问题很明显 - doneTap
调用performSegue
并将sender
设置为nil
:
@IBAction func doneTapp(_ sender: Any) {
self.performSegue(withIdentifier: nearbySearchSegueIdentifier, sender: nil)
}
在prepareForSegue
中,您在目标视图控制器上设置类别,但在此之前,您有一个guard
语句,检查sender
是否为选定类别。由于在这种情况下您已将sender
设置为nil
,因此该守护将导致代码跳过该类别的设置,并且您的代码行为就好像您还没有设置类别一样(因为你真的没有设置它。)
覆盖func prepare(对于segue:UIStoryboardSegue,sender:Any?){
if segue.identifier == nearbySearchSegueIdentifier {
guard let category = sender as? QCategoryy else {
return
}
if let selectedRows = tableView.indexPathsForSelectedRows {
if let vc : CourseClass2 = segue.destination as? CourseClass2 {
vc.category = category
}
}
}
}
我的建议是删除guard
,而是使用tableView.indexPathsForSelectedRows
来确定要发送给下一个视图控制器的类别,并使用它们,例如:
// this will get you the list of selected categories, or if none is selected, an empty list
let selectedCategories: [QCategoryy] = tableView.indexPathsForSelectedRows?.map({ (indexPath) -> QCategoryy in
return list[indexPath.row]
}) ?? []
vc.categories = selectedCategories