I have been banging against this for days now, trying a number of different approaches. I have a UITableView with custom cells containing two UILabels. On selecting the cell, a UIPicker is revealed, and a variable is set referencing which cell was tapped.
Once a selection is made in the UIPicker, I need to take that value and use it to replace the existing .text of one of the cell labels. I have tried using .viewWithTag a few different ways (first tagging the label itself, then the entire cell) and nothing works.
My didSelect for the picker is:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("pickerView did select row \(row)")
if(self.getStatus() == "newFirst"){
switch (editingField){
case 0:
var l:NewOrderCell! = tableView.viewWithTag(editingField) as? NewOrderCell
//the label in the custom cell is called 'Text'
l.Text.text = pickerData[row]
println("should be changing \(l.Text.text) into \(pickerData[row])")
default:
var l:NewOrderCell! = tableView.viewWithTag(editingField) as? NewOrderCell
l.Text.text = pickerData[row]
}
}
picker.hidden=true
}
and my cellForRowAtIndexPath for the table is:
var cell:NewOrderCell = tableView.dequeueReusableCellWithIdentifier("NewOrderCell") as NewOrderCell
//label
cell.Label.text = self.newOrder[indexPath.row]
//text
cell.Text.text = "Please Select"
cell.tag = indexPath.row
return cell
I know my goal, but can't get a handle on the proper way to reference a cell within a table from another method. Thanks for your help
I have come up with a solution that works, but I feel is inelegant and hard to maintain: when the picker row is selected, that text is written to the array that was used to originally populate the table (overwriting the original item), and then I reload the table, so that the new value appears. So, it's something like this:
var truth: [String:String] = ["Key1":"","Key2":"","Key3":""]
var keys:[String] = ["Key1", "Key2","Key3"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:NewOrderCell = tableView.dequeueReusableCellWithIdentifier("NewOrderCell") as NewOrderCell
//label
cell.Label.text = keys[indexPath.row]
//textfield
if(truth[keys[indexPath.row]] == ""){
cell.Text.text = "Please Select"
}
else{
cell.Text.text = truth[keys[indexPath.row]]
}
return cell
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
truth[keys[editingField]] = pickerData[row]
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
Happy to hear better solutions
Related
The first text field is a Int Value where you insert your current sold. A second one textfield where I get some Int value and add to a tableView with a button. The value you add into the second textfield soustract to the main value ( in that case 2500 ). That's working great.
My problem is to attribuate the PickerValue to the TableRow Value. it's working but not like I want.
I want to save the pickerValue when i click onto the button. But when i enter a new value it's changing all row of the pickerValue. In that case, you can see two row with "telephone". Normaly it's would be "telephone" and something else ( "maison" or "house").
Do you see a reason why it's doing like this ? I wish you can understand what i mean.
problem with picker value save into table view (gif)
#IBAction func addBtn(_ sender: Any) {
view.endEditing(true)
salaire = Int(salaireLabel.text!) ?? 0
valeur = Int(ressourceTextField.text!) ?? 0
if String(valeur) != "" {
addBtnActivated = true
restValueLabel.textColor = UIColor.blue
arrayRessource.append(valeur)
// modifie automatiquement le salaire
restValueLabel.text = String(soustraction)
tableRessourceOT.reloadData()
// reset le champs
ressourceTextField.text = ""
picker.selectRow(0, inComponent: 0, animated: true)
}
}
Here is some code I'm using. I'm thinking the problem came from the tableview Func
var arrayPickerValue: [String] = ["", "maison", "telephone"]
// ------------------------------ START PICKER ------------------------------------
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
print("-----arraypicker.count------")
print(arrayPickerValue.count)
return arrayPickerValue.count
}
// hauteur du picker pour que les images ne se supperpose pas / Picker height
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 25
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print("----arraypicker[row]----")
print(arrayPickerValue[row])
return arrayPickerValue[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
stored = arrayPickerValue[row]
print("-----stored dans func didselectrow------")
print(stored as Any)
}
//-------------------------- END PICKER -----------------------------------
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let row = indexPath.row
let value = arrayRessource[row]
cell.textLabel?.text = "- " + String(value) + " €"
// cell.detailTextLabel?.text =
updateRestLabel()
setRestLabel()
return cell
}
Here is my outlet :
#IBOutlet weak var tableRessourceOT: UITableView!
Then my arrayRessource :
var arrayRessource: [Int] = [] {
didSet {
if oldValue != arrayRessource {
userDefault.setValue(arrayRessource, forKey: arrayKey)
}
}
}
and call with the function :
func getArray() {
if let newArray = userDefault.array(forKey: arrayKey) as? [Int] {
arrayRessource = newArray
}
}
Your question is not very clear.
However I will make an attempt to offer some help from what I can extract from your code.
I assume restValueLabel is not something within your cells? It is probably the green label "2500" in your screenshot?
If that is the case, don't set it in cellForRow(at:). This method is called every time a cell becomes visible and should only be used to configure that very cell based on your data source (arrayRessource).
Instead create a method updateRestValueLabel() that iterates your data source, does the calculation and sets the appropriate value to your restValueLabel. Call that method every time your data source changes, so along with tableRessourceOT.reloadData().
There are also some stylistic issues with your code (e.g. avoid force unwrapping ! unless you have good reason to use it) but this is out of scope here.
The problem come from my array was an Int array and my pickerValue was String array.
Array can't be String array's and Int array's.
var arrayRessource: [String] = [] {
didSet {
if oldValue != arrayRessource {
userDefault.setValue(arrayRessource, forKey: arrayKey)
}
}
}
Thanx for your advice by the way i'm progressing and doing my best
enter image description here My requirement is to show the users all the selection options to the user so that he can see all at once an then able to select from those just like it is done with spinner view in android. Currently I am using UI picker view. I am attaching the screen shot of it. Either I want a spinner or want the picker view to show 10 items simultaneously for the same height where it is currently showing three. This is my code:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var myString = view as! UILabel!
var myStringLength = 0
if( view == nil) {
myString = UILabel()
}
myString?.textAlignment = .center
var rowText:String
if(pickerView==District){
rowText=districts[row]
}
else if(pickerView==block)
{
rowText=blocks[row]
}
else if(pickerView==management)
{
rowText=managements[row]
}
else
{
rowText=categories[row]
}
var attributedRowText = NSMutableAttributedString(string: rowText)
var attributedRowTextLength = attributedRowText.length
attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: attributedRowTextLength))
attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 8.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))
myString!.attributedText = attributedRowText
return myString!
}
You can use table view as a dropdown:
subCategoryTable is table name. Create this table on press of that dropdown button. here subCatgBtn is that btn on click write this code.
subCategoryTable.frame = CGRectMake(self.subCatgBtn.frame.origin.x, self.subCatgBtn.frame.origin.y + self.subCatgBtn.frame.size.height, self.subCatgBtn.frame.size.width,CGFloat(numberofrows * 2));
subCategoryTable.delegate = self
subCategoryTable.dataSource = self
subCategoryTable.tag = 2
//subCategoryTable.rowHeight = self.subCatgBtn.frame.size.height
subCategoryTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")//this is needed when you use custom cell otherwise no need
subCategoryTable.separatorColor = UIColor.lightGrayColor()
subCategoryTable.allowsSelection = true
self.view.addSubview(subCategoryTable)
CellforrowIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// var cell = tableView.dequeueReusableCellWithIdentifier("cell")!
if tableView.tag == 2
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
//do whatever you want to show.
}
}
numberofrows method:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 2
{
districts.count
}
}
and on Didselect method just set the name on that button whatever you selected.
I dont know Your UI how you designed, You can use this one as dropdown it will create dynamically when you click on that button.
There isn't anything like spinner in the cocoatouch framework.
At most what you can do is have a custom implementation with UItableView
or you can also use something like NIDropdown which will help in your implementation.
I'm using a UITableViewCell prototype that contains a UIPickerView and use that prototype for 4 different cells with 4 different PickerView in the tableView. I use the following code to supply the cell to tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) and to set each picker to a different instance variable in order to differentiate between the pickers later (since the same UITableViewController instance is the delegate/datasource for all of them, for example).
However, when running the code, all 4 instance variables end up pointing to the same UIPickerView. How can I ensure that it uses 4 distinct UIPickerViews instead?
func PickerCell(tableView: UITableView, indexPath: NSIndexPath, inout picker: UIPickerView?) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PickerCell")
if let pickerFromCell = cell?.contentView.viewWithTag(1) as? UIPickerView {
pickerFromCell.reloadAllComponents()
pickerFromCell.dataSource = self
pickerFromCell.delegate = self
picker = pickerFromCell
}
return cell!
}
Instead of using tag try something like this. Change your didSelectRow of PickerViewDelegate like this
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
}
Also add pickerSelectedIndexArr array in your file and assign it in viewDidLoad like following way
var pickerSelectedIndexArr: [Int] = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
self.pickerSelectedIndexArr = [0, 0, 0 ,0]
}
Now you can easily get all the picker selected value any time you want
Hope this will help you.
var dict: [Int, UIPickerView]
...
if dict[indexPath.row] == nil {
// Create UIPickerView and assign it to dict[indexPath.row].
}
As this question is tagged with Objective C, I am giving your answer in OBJC.
Here is what you can do, to deal with controls in reusable cell,
Tip for solving such problem, when you have controls in UICollectionViewCell or UITableViewCell, give tag to your controls depending upon your indexPath.
Giving example with collectionViewCell you can do it with tableViewCell
case 1 : If CollectionView is sectional, then your tags will be like [0][0], [1][0] ... In such case do something like this,
collectionViewCell.picker.tag = [[NSString stringWithFormat:#"%ld%ld",(long)indexPath.section,(long)indexPath.item] intValue]; // If it is sectional collection view
case 2 : If Collection View is non-sectional,do something like this,
collectionViewCell.picker.tag = [[NSString stringWithFormat:#"%ld",(long)indexPath.item] intValue]; // If it is non-sectional collection view
Hope this will solve your problem or give you idea to manage accordingly.
If you have more than one control in your cell then just give tags like indexPath.item + 1 + 2 ...
I agree with Nirav, that you should use the cell to determine the NSIndexPath of the cell in question.
Personally, I'd make the cell subclass the data source and delegate of the picker and have it take responsibility for the picker. This completely eliminates any confusion about what picker is associated with which cell. But I'd have a protocol by which the cell can inform the view controller when the user selected a value from the picker and the view controller can update the model.
For example, consider this UITableViewCell subclass:
// 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)
}
}
And then the view controller:
// 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
}
}
}
For start how I've created expandable cell with UIPicker. Just in case that would be relevant to issue.
It's created in UITableView by this code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var identifyer = ""
switch(indexPath.row){
//other cells
case 1:
//Kategoria
identifyer = "category"
categoryCell = tableView.dequeueReusableCellWithIdentifier(identifyer) as? CategoryTableViewCell
categoryCell.pickerView.delegate = self
categoryCell.pickerView.dataSource = self
return categoryCell
//other cells
}
Then I have to recognize if it's touched
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row == 1){
isCategoryCellSelected = true
tableView.reloadRowsAtIndexPaths([categoryIndexPath()], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
That's how I replace text in UILabel
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
categoryCell.categoryNameLabel.text = categories[row]
}
And finaly when tableView refresh cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch(indexPath.row){
case 1:
if (isCategoryCellSelected){
return CategoryTableViewCell.expandedHeight
} else {
return CategoryTableViewCell.defaultHeight
}
//other heights
}
}
Defalut cell looks
Expanded cell looks
ISSUE
So when I choose item in picker then label above should have change it text and that is happening. However, when this cell shrink to default height then replace effect is gone. I have to scroll down and up to see that only then I can see changed text in label.
I assume that UITableView cache this cell and when cell is reloading then it takes this cached version. I'm only guessing. Is it how I think? How I can change this unwanted action?
SOLUTION
As #the_critic pointout my approche to save cells in vars was completly wrong. In same time recreating categoryCell every time I pick row wasn't the correct way to do it. I end up with his way of creating cell but with mine way of setting value to cell.
So it looks like this what's is chagned:
creating cell
categoryIdentifier is private let String
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell
switch(indexPath.row){
// other cells
case 1:
//Kategoria
print("recreate, selected category \(selectedCategory)")
let categoryCell = tableView.dequeueReusableCellWithIdentifier(categoryIdentifier) as! CategoryTableViewCell
categoryCell.pickerView.delegate = self
categoryCell.pickerView.dataSource = self
updateSelectedCategoryIfNeeded(categoryCell)
cell = categoryCell
// other cells
return cell;
}
private func updateSelectedCategoryIfNeeded(cell:CategoryTableViewCell) {
if let selectedCategory = self.selectedCategory{
// A category has been selected!
cell.categoryNameLabel.text = selectedCategory
updatePicekerRowPosition(cell)
}else{
// no category selected!
cell.categoryNameLabel.text = "Wybierz kategorie..."
}
}
private func updatePicekerRowPosition(cell:CategoryTableViewCell) {
if let index = categories.indexOf(selectedCategory!){
cell.pickerView.selectRow(Int(index.value), inComponent: 0, animated: false)
}
}
recognizing row selecting
categoryIndexPath is private let NSIndexPath
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedCategory = categories[row]
if let categoryCell = tableView.cellForRowAtIndexPath(categoryIndexPath) as? CategoryTableViewCell {
categoryCell.categoryNameLabel.text = selectedCategory
}
}
From what I can see in your code, there are quite a few things you misunderstand about table views.
I would try to say that more politely, but I can't. Your way of referencing a categoryCell in your code is completely wrong! UITableviewCells are not static references if you dequeue them!
FIRST STEP: Remove the categoryCell variable!!!
The way the table view works is the following:
The cellForRowAtIndexPath method takes a cell from the storyboard or your nib and reuses that cell over and over again! So, in the beginning, you may get away with doing it your way (creating a reference to categoryCell), but the situation changes as soon as you have more cells than fit on the screen, because the variable will reference a different cell!
Reading recommendation: Creating and Configuring a Table View
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell?
switch(indexPath.row){
//other cells
case 1:
//Kategoria
let identifier = "category"
// tableview checks if there is a cached cell
// for the identifier (reuse),
// if there is, it will take that one!
cell = tableView.dequeueReusableCellWithIdentifier(identifier) as! CategoryTableViewCell
cell.pickerView.delegate = self
cell.pickerView.dataSource = self
if let selectedCategory = self.selectedCategory{
// A category has been selected!
cell.categoryNameLabel.text = selectedCategory
}else{
// no category selected!
cell.categoryNameLabel.text = "Wybierz kategorie..."
}
return cell
//other cells
}
As you can see above, I introduced a new variable called selectedCategory, which will reflect which category is currently selected...
Set it up like this in your controller:
var selectedCategory : String?
What happens when you reload a section or row or the whole table, is that all the UITableViewDataSource methods for the given rows are called again! In a way the table view always tries to reflect some state of your model.
You should always reflect changes in your model by reloading the row/section or the whole table (depending on what changed).
So when you pick your category, you change the model and reload your row!
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// update the model!
selectedCategory = categories[row]
// the table view now needs to know that the model changed!
// this will trigger the dataSource method cellForRowAtIndexPath
// and because it selectedCategory is now set, it will update
// your string accordingly!
tableView.reloadRowsAtIndexPaths([/*<IndexPath of your categoryLabelCell>*/], withRowAnimation: UITableViewRowAnimation.Automatic)
}
Phew! I hope that helps...
I am currently working on a small project and i have a viewController that has 4 textFields which 3 work ok. They take String objects. However, the 4th textField is supposed to bring up a UIPickerView with 4 selectable items.
So far this is what i have in my controller that implements this:
#IBOutlet var pickerTextfield: UITextField!
#IBOutlet var itemPicker: UIPickerView! = UIPickerView()
The pickerTextfield is the UITextField object that is the 4th field.
The itemPicker is an unlinked UIPickerView that i want to create programatically.
Right below these properties, i have an array of items for the UIPickerView object:
var seasonalItems = ["Spring", "Summer", "Fall", "Winter"]
In my viewDidLoad method i have this as follow:
itemPicker.hidden = true;
pickerTextfield.text = seasonalItems[0]
pickerTextfield.delegate = self
And the rest of the implementation:
// Below these lines is the implementation of the Picker
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return seasonalItems.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return seasonalItems[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
pickerTextfield.text = seasonalItems[row]
itemPicker.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false
}
So the end result from this is when i tap the pickerTextfield object in the app, it shows the first item of the array (Spring) but in text within the UITextField object but it does not show the UIPickerView object with the other selectable items where i could select one and then hide it when selected.
My question is, where or what am i doing wrong here? i been trying to figure this out on my own but i do not seem to get good clear examples with Swift and storyboards. I much rather not drag a UIPickerView in the storyboard but rather the way i attempted to implement. Thanks
You can give UIPickerView as inputView for your TextField in which you want to show picker view.
You also do not need to initially hide picker view in this case.
pickerTextfield.inputView = itemPicker
When you use UIPickerView as inputView of any UITextField then when you tap on the TextField instead of default keypad PickerView will show.