I've got an Array that I'm using to populating the rows of a UITableView.
After a row is selected I need to retrieve information from the Array based on the row selected to populate some outlets (labels, textfields, etc.)
For example:
I create an itemSelected variable in the didSelectRowAtIndexPath in my ViewController for the TableView which I set to indexPath.row
itemSelected = indexPath.row
Then in my viewDidLoad for my otherViewController I need to retrieve the info by
array[itemSelected]
But, I get a compiler error that says: "Expression resolves to unused i-value"
In here you simply accessing the array but not calling any value. As a example if you have a key call "Name" in your array and you want to set it to a UILabel just do it as this.
self.Name.text = array[itemSelected].valueForKey("Name") as! String
if not just do something with it.
self.Name.text = array[itemSelected] as! String
OR
print(array[itemSelected])
Related
I want to replace the original tableview with collectionview, the code of the original tableview:
let selectedRow = MarketView.indexPathForSelectedRow!.row
I'm learning online to change to this code and I get an error:
let selectedRow = MarketView.indexPathsForSelectedItems!.first
The error shows:
Cannot convert value of type 'IndexPath?' to expected argument type 'Int'
This is the complete code as shown in the figure
I just learned to use collectionview, how should I modify it, thank you for your help
Unlike indexPathForSelectedRow which returns a single index path indexPathsForSelectedItems returns an array of index paths
And row is not first, the collection view equivalent of row – as the name of the API implies – is item, you have to write indexPathsForSelectedItems!.first!.item.
But it's not recommended to force unwrap the objects. A safer way is
guard let selectedRow = MarketView.indexPathsForSelectedItems?.first?.item else { return }
So I have retrieved data from firebase and stored it in a dictionary.
it looks somewhat like this:
["kmFcP8NxxnpqZ8rcya9amPfAFFzSz1": xyz#gmail.com,
"75M2moeHxxXHSXq09jki0sl7hiEbd2": xyz#gmail.com,
"iX8othUunxxxUuCDpkIjO7rRBnvu32": xyz#gmail.com]
now I want to add these values in a UiTableView. The cell only contains a label on which I have to show these values.
if the keys wouldn't have been random and maybe had a name, suppose "name" .I could have done something like this
cell.label.text = dict[indexpath.row]["name"] as! String
but since the keys are random, how can I do it?
First you have to get all keys in a separate array because dictionary is hashable and indexPath won't work in it. for that you can do
let keys = dictionary.keys()
In numberOfItems in section use return keys.count
Later in cellForRowAt indexPath you can acheive something like this
cell.label.text = dict[keys[indexpath.row]] as! String
What it'll do is to find the key associated to the row in dictionary. and it is completely dynamic
My issue is this...when I click on a row of my tableview, I have to go to another screen. In that screen, there is a textfield where the value in the row(in previous screen) has to be assigned to a textfield..for that I am doing this..
editTextField.text = modelArray[index!]
But it shows this error…’Cannot assign value of type 'NSManagedObject' to type 'String?'’
Also modelArray is of type NSManagedObject.
What should be done in this case...?
It's a classic type mismatch. You have NSManagedObject but the label expects String
Two options:
If you are using NSManagedObject subclass and modelArray is declared as an array of that type
let item = modelArray[index!]
editTextField.text = item.categoryName
If you don't use NSManagedObject subclass
let item = modelArray[index!]
editTextField.text = item.value(forKey:"categoryName") as? String
In any case why is index an optional?. An optional index type is nonsense because it doesn't prevent an out-of-range exception.
’Cannot assign value of type 'NSManagedObject' to type 'String?'’
This show's you are trying to use coredata value to show in the textfiled. You can do these things
Pass the value from tableview didselect method to controller and show that in the textfield (the value in the cell definitely be a string)
Create Textfield globally in your nextviewcontroller and change its value in tableview did select delegate method
I have written code that filters events from Core Data and only prints the events that have a date attribute that is equal to a date that was selected in a Calendar. Heres the code:
let predicate = NSPredicate(format: "eventPastStringDate = %#", formatter.stringFromDate(selectedDate))
//This prints the exact data that I want
print((eventsPast as NSArray).filteredArrayUsingPredicate(predicate))
This works and it filters that data how I would like and it prints only the events that I want. The problem is that I do not know how to display this data in the tableView. Usually I can display all the data from Core Data in the tableView like this in cellForRowAtIndexPath...
let ePast = eventsPast[indexPath.row]
let cellPast = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
// Sets labels in cell to whatever user typed in on the events page
cellPast.titlePrototypeCell!.text = ePast.eventPastTitle!
cellPast.datePrototypeCell!.text = "Date: " + ePast.eventPastDateLabel!
return cellPast
...but I am not sure how to access the new data as an array like I did above. Any ideas?
You are going to have a second array in your class. When there is no filter, the two arrays are identical. When there is a filter, then you have a master array with absolutely everything and the filtered array is a small subset.
(class var) myFilteredArray = (eventsPast as NSArray).filteredArrayUsingPredicate(predicate)
Then in your tableview methods:
let ePast = myFilteredArray[indexPath.row]
And make sure to set the table row size to your filtered array count, not the master array count. Otherwise you are going to get out of bounds crashes.
I have an array that becomes loaded with objects when I call it in my viewDidLoad method. I know that it is not empty because I am able to print the data and the count of the array when I call it in viewDidLoad.
However, when I try to take the data out of the array and put it into a text view, the array appears to be empty. The code below is where I attempt to add the items from requestList2 into a textfield in tableView:cellForRowAtIndexPath:.
Why does does my array possess data when I call it in viewDidLoad, but not when I try to add it to cell.usernameOfRequestSender.text?
if (requestList2.count > 0){
var user = requestList2.objectAtIndex(indexPath.row) as! String
cell.usernameOfRequestSender.text = user
}
return cell