我正在使用包含UITableViewCell
的{{1}}原型,并将该原型用于UIPickerView
中4个不同PickerView
的4个不同单元格。我使用以下代码将单元格提供给tableView tableView
并将每个选择器设置为不同的实例变量,以便稍后区分选择器(因为相同的(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
实例是委托/数据源所有这些,例如)。
但是,在运行代码时,所有4个实例变量最终都指向同一个UITableViewController
。如何确保它使用4个不同的UIPickerView
代替?
UIPickerViews
答案 0 :(得分:1)
而不是使用POSITION_NONE
尝试这样的事情。像这样更改tag
的{{1}}
didSelectRow
在您的文件中添加PickerViewDelegate
数组,并按照以下方式在func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let cell = imageView.superview?.superview as! UITableViewCell //You have to use as may super as your UITableViewCell hierarchy
let indexPath = self.tabelView.indexPathForCell(cell)
self.pickerSelectedIndexArr[indexpath.row] = row
}
中分配
pickerSelectedIndexArr
现在,您可以随时轻松获取所有选择器选择的值 希望这会对你有所帮助。
答案 1 :(得分:0)
var dict: [Int, UIPickerView]
...
if dict[indexPath.row] == nil {
// Create UIPickerView and assign it to dict[indexPath.row].
}
答案 2 :(得分:0)
由于此问题标有Objective C
,我将在OBJC中给出答案。
以下是您可以执行的操作,以处理可重用单元格中的控件,
解决此类问题的提示when you have controls in UICollectionViewCell or UITableViewCell, give tag to your controls depending upon your indexPath
。
使用collectionViewCell
给出示例,您可以使用tableViewCell
进行操作
案例1:如果CollectionView是截面的,那么你的标签就像[0] [0],[1] [0] ......在这种情况下做类似的事情,
collectionViewCell.picker.tag = [[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.item] intValue]; // If it is sectional collection view
案例2:如果集合视图是非截面的,请执行以下操作,
collectionViewCell.picker.tag = [[NSString stringWithFormat:@"%ld",(long)indexPath.item] intValue]; // If it is non-sectional collection view
希望这可以解决您的问题或让您相应地进行管理。 如果你的单元格中有多个控件,那么只需给出indexPath.item + 1 + 2 ...
等标签答案 3 :(得分:0)
我同意Nirav的意见,您应该使用该单元格来确定相关单元格的NSIndexPath
。
就个人而言,我将单元格子类化为选择器的数据源和委托,并让它对选择器负责。这完全消除了关于哪个拾取器与哪个单元相关联的任何混淆。但是我有一个协议,当用户从选择器中选择一个值并且视图控制器可以更新模型时,单元可以通过该协议通知视图控制器。
例如,考虑这个UITableViewCell
子类:
// CustomCell.swift
import UIKit
/// Protocol that the view controller will conform to in order to receive updates
/// from the cell regarding which row in the picker was picked.
///
/// - note: This is a `class` protocol so that I can use `weak` reference.
protocol CustomCellDelegate: class {
func cell(customCell: CustomCell, pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
}
/// Custom cell subclass, which is the picker's data source and delegate
class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate {
/// The delegate who we will inform of any picker changes.
///
/// This is weak to avoid strong reference cycle.
weak var delegate: CustomCellDelegate?
/// The array of values to be shown in the picker.
///
/// If the `values` changes, this reloads the picker view.
var values: [String]? {
didSet {
pickerView.reloadComponent(0)
}
}
/// The outlet to the picker in the cell.
@IBOutlet weak var pickerView: UIPickerView!
// MARK: UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return values?.count ?? 0
}
// MARK: UIPickerViewDelegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return values?[row] ?? ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
delegate?.cell(self, pickerView: pickerView, didSelectRow: row, inComponent: component)
}
}
然后是视图控制器:
// ViewController.swift
import UIKit
class ViewController: UITableViewController, CustomCellDelegate {
// an array of arrays of acceptable values
let troupes = [
["Mo", "Larry", "Curly"],
["Abbott", "Costello"],
["Groucho", "Harpo", "Zeppo", "Chico", "Gummo"],
["Laurel", "Hardy"],
["Graham Chapman", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones", "Michael Palin"]
]
/// An array that indicates which is item is selected for each table view cell
var selectedItemForRow: [Int]!
override func viewDidLoad() {
super.viewDidLoad()
selectedItemForRow = troupes.map { _ in return 0 } // initialize the `selectedItemForRow` array with zeros
// Whatever further initialization of the view controller goes here.
}
// MARK: UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return troupes.count
}
// Populate cell, setting delegate, list of acceptable values, and currently selected value.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
cell.delegate = self
cell.values = troupes[indexPath.row]
cell.pickerView.selectRow(selectedItemForRow[indexPath.row], inComponent: 0, animated: false)
return cell
}
// MARK: CustomCellDelegate
// When the cell tells us that the user changed the selected row, let's update our model accordingly.
func cell(customCell: CustomCell, pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let indexPath = tableView.indexPathForCell(customCell) {
selectedItemForRow[indexPath.row] = row
}
}
}