Ios 如何比较两个不同UIPickerView的值?
我有两个自定义UITableViewController和两个自定义UITableViewCell 它们中的每一个都包含UIPickerView 我想比较这些UIPickerViews的值 (我制作地图应用程序,我想制作路线,所以我需要比较两个点,如果它们不同,“制作路线”按钮将设置为启用状态) 我该怎么做?我尝试使用didSet值,但没有成功 以下是我的课程:Ios 如何比较两个不同UIPickerView的值?,ios,swift,xcode,uikit,Ios,Swift,Xcode,Uikit,我有两个自定义UITableViewController和两个自定义UITableViewCell 它们中的每一个都包含UIPickerView 我想比较这些UIPickerViews的值 (我制作地图应用程序,我想制作路线,所以我需要比较两个点,如果它们不同,“制作路线”按钮将设置为启用状态) 我该怎么做?我尝试使用didSet值,但没有成功 以下是我的课程: class PlaceForRouteCell: UITableViewCell, UIPickerViewDelegate, UIP
class PlaceForRouteCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
// Выбираем все места в базе данных
let places = realm.objects(Place.self)
var selectedPlace = Place()
func getSelectedPlace() -> Place {
return selectedPlace
}
//число "барабанов"
func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 }
//число элементов в "барабане"
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//если БД пуста, то предупредить об этом
return places.count != 0 ? places.count : 1
}
//содержимое "барабанов"
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (places.count != 0) {
if (places.count == 1) { return "Добавьте еще одно место" }
return places[row].userName
}
return "Сохраненных мест нет"
}
//обрабатываем выбранный элемент
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (places.count != 0) {
print(picker.tag)
selectedPlace = places[row]
// print("выделена строка №\(row), элемент \(places[row])'")
}
}
func setup() { picker.delegate = self }
}
及
路由设备控制器的类表:UIViewController、UITableViewDataSource、UIPickerViewDeleteGate{
// Выбираем все места в базе данных
让places=realm.objects(Place.self)
@IBOutlet弱var MaketheroutButton:UIBarButtonim!
@IBVAR表格视图:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
tableView.tableFooterView=UIView(帧:CGRect(x:0,y:0,
宽度:tableView.frame.size.width,height:1)
tableView.dataSource=self
将cell=tableView.cellForRow(at:indepath(行:0,节:1))设为!PlaceForRouteCell
cell.picker.selectRow(1,不完整:0,动画:true)
var cellA=tableView.cellForRow(at:indepath(行:0,节:0))as!PlaceForRouteCell{
迪塞特{
印刷品(印刷品)
}
}
var cellB=tableView.cellForRow(at:indepath(行:0,节:1))as!PlaceForRouteCell
//打印(“A:\(cellA.picker)的标记”)
//打印(“B:\(cellB.picker.tag)的标记”)
var valueOfA=cellA.getSelectedPlace(){
迪塞特{
印刷品(“印刷品”)
}
}
打印(valueOfA.userName)
var valueOfB=cellB.getSelectedPlace(){
迪塞特{
印刷品(“印刷品”)
}
}
打印(valueOfB.userName)
如果(places.count<2){makeTheRouteButton.isEnabled=false}
}
@iAction func使路由按钮未按下(uu发件人:UIBarButtonim){
将cellA=tableView.cellForRow(at:indepath(行:0,节:0))设为!PlaceForRouteCell
设pointA=cellA.selectedPlace
将cellB=tableView.cellForRow(at:indepath(行:0,节:1))设为!PlaceForRouteCell
设pointB=cellB.selectedPlace
打印(“点A:\(点A.userName),点B:\(点B.userName)”)
}
//变量arePlacesEqual:Bool=比较(){
//迪塞特{
//印刷品(“印刷品”)
// }
// }
func comparing()->Bool{
将cellA=tableView.cellForRow(at:indepath(行:0,节:0))设为!PlaceForRouteCell
将cellB=tableView.cellForRow(at:indepath(行:0,节:1))设为!PlaceForRouteCell
让valueOfA=cellA.selectedPlace.userName
让valueOfB=cellB.selectedPlace.userName
如果(valueOfA==valueOfB){return true}
返回错误
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{return 1}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“placeForRouteCell”,for:indexath)作为!placeForRouteCell
cell.picker.tag=indexPath[0]
cell.setup()
返回单元
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
道岔(区段){
案例0:返回“ччаА”
案例1:返回“ччаБ”
默认值:返回“”
}
}
// количество секциий
func numberOfSections(在tableView:UITableView中)->Int{return 2}
}
我用单元格中的pickerView创建了
表格视图。
在didSelectRow
方法中,我添加了NotificationCenter
sender
我的tableViewCell类:
let picker_array = ["one", "two", "three"]
class TableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.picker.delegate = self
self.picker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return picker_array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(picker_array[row])"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
NotificationCenter.default.post(name: Notification.Name("Notification"), object: nil)
}
}
在viewDidLoad
中,我添加了NotificationCenter
listener。
我的ViewController类:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let dummy_data = [1,2]
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(methodOfReceivedNotification(notification:)), name: Notification.Name("Notification"), object: nil)
}
@objc func methodOfReceivedNotification(notification: Notification) {
for index in dummy_data {
let indexPath = IndexPath(row: index - 1, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
let selectRow = picker_array[cell.picker.selectedRow(inComponent: 0)]
print(selectRow)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummy_data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
return cell
}
}
当我更改pickerView时,控制台将从单元格打印当前值
我想这会有帮助是的,当我点击按钮时,我可以检查这一点,但这不是我的情况,我想每次检查这两个选择器,如果它们中的一些获得新值,如果两个值相等,然后我将我的按钮设置为“禁用”状态,如果它们不同,那么按钮就是enabled@alexandertopskiy,我编辑了我的答案,请检查谢谢!!!虽然做了一些改变,但还是奏效了!!!
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let dummy_data = [1,2]
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(methodOfReceivedNotification(notification:)), name: Notification.Name("Notification"), object: nil)
}
@objc func methodOfReceivedNotification(notification: Notification) {
for index in dummy_data {
let indexPath = IndexPath(row: index - 1, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
let selectRow = picker_array[cell.picker.selectedRow(inComponent: 0)]
print(selectRow)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummy_data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
return cell
}
}