TableView 限制使用 (Swift)
Posted
技术标签:
【中文标题】TableView 限制使用 (Swift)【英文标题】:TableView limit use (Swift) 【发布时间】:2017-10-23 09:44:16 【问题描述】:您好,我有两个 viewController,第一个带有步进器
var steppers : UIStepper?
@IBOutlet weak var hourLabel: UILabel!
@IBAction func stepper(_ sender: UIStepper)
hourLabel.text = String(sender.value)
第二个带有tableView
import UIKit
import AVFoundation
class SelectClass: UIViewController, UITableViewDelegate, UITableViewDataSource
@IBOutlet weak var tableView: UITableView!
var list : [QCategoryy] = [QCategoryy]()
var audioPlayer : AVAudioPlayer!
var limit = 3
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()
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
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 = 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 中,我可以选择多个单元格并使用 var 为选择添加限制
var limit = 3
和函数
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
但我想做的是使用第一个 viewController 中的步进器在 tableView 的选择处添加限制(在第二个 viewController 中),类似于
我该怎么做?
【问题讨论】:
然后通过表格视图将值传递给VC。有很多帖子都在谈论向 VC 传递东西 @Sweeper 如果你已经知道,你甚至可以写它 好的,如何从带步进器的 VC 过渡到带表视图的 VC?赛格斯? @Sweeper 是的,有segues 【参考方案1】:在带有步进器的 VC 中,覆盖 prepareForSegue
:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if let vc = segue.destination as? SelectClass
vc.limit = Int(stepper.value)
编辑:您需要使步进器成为 VC 的出口,首先将声明更改为:
@IBOutlet var stepper: UIStepper!
在情节提要中,右键单击您的视图控制器(带有黄色图标的那个):
然后出现这个:
看到“奥特莱斯”下的stepper
了吗?从stepper
旁边的圆圈拖动到情节提要上的步进器:
【讨论】:
它给了我错误“可选类型'UIStepper的值?'没有打开;你是不是要使用“!”或者 '?'?”如果我添加“!”或者 '?'应用程序崩溃并出现错误:“致命错误:在展开可选值时意外发现 nil” @seran 您需要将 stepper 属性与情节提要中的 stepper 连接起来。 我该怎么做? (对不起,我是初学者)以上是关于TableView 限制使用 (Swift)的主要内容,如果未能解决你的问题,请参考以下文章
Swift 3:Tableview 选择行转到不同的导航视图
Swift UITableView 删除行项 - tableview 数据源是 Core Data