Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Using dictionary instead of array for disclosure button
On my newbie journey with iOS development, I have managed to get my UITableView drawing out cells from an Array. This though isn't very flexible for when I need to pass an ID to a detail view.
I have setup a dictionary to handle the key value pairs that I needed and I'm now trying to get the accessoryButtonTappedForRowWithIndexPath to reference the corresponding key. The problem that I am having is that the value being passed in is always a 1 (which makes sense as I have the enumerator nextobject), the closet thing I can think of is something similar to how I did this with arrays;
I'm not sure I understand what you are trying to do. Why are you using a dictionary, which is an unordered collection, with a tableview which is ordered (indexed)? Also, why are you creating a new dictionary each time the accessory button is tapped? Also, I don't see you releasing editRecord anywhere, so you're leaking it.
EDIT: One more point, the convention is to name classes with a capital letter, so addUS should be AddUS.
I'm not sure I understand what you are trying to do. Why are you using a dictionary, which is an unordered collection, with a tableview which is ordered (indexed)? Also, why are you creating a new dictionary each time the accessory button is tapped? Also, I don't see you releasing editRecord anywhere, so you're leaking it.
EDIT: One more point, the convention is to name classes with a capital letter, so addUS should be AddUS.
Apologies, I this is a dummy project I'm playing with to get to grips with the code.
I'm creating a new dictionary as I have problems passing a dictionary between methods (why I have that problem I'm still working on). I am [editRecord release] I just omitted it from the code.
Ultimately what I want to do is associate a key pair to a cell and use the ID I set against the disclosure button.
Do I need to create an array and put dictionaries in them? It seems a little bloated, but its an approach someone else mentioned.
You could easily solve your first problem by creating an instance variable in your class for the dictionary (and a property for it).
But if your cells require more then a simple array for their data, I suggest creating a container class for the data and using an array of those objects for the source.
For example, imagine you want to display, in each cell a persons: Name, Address, and in case a Phone number is available, show a disclosure button which would dial the number. You would start by creating a new class (subclass of NSObject) with three NSString properties (Name, Address, Phone). Your date source would then be an array of instances of this Person object, one object per cell. In the cellForRowAtIndex button, if a phone number is available you would show the disclosure. In accessoryButtonTappedForRowWithIndexPath you would grab the object with the indexPath.row and dial the number stored in that object.
Apologies, I this is a dummy project I'm playing with to get to grips with the code.
I'm creating a new dictionary as I have problems passing a dictionary between methods (why I have that problem I'm still working on). I am [editRecord release] I just omitted it from the code.
Ultimately what I want to do is associate a key pair to a cell and use the ID I set against the disclosure button.
Do I need to create an array and put dictionaries in them? It seems a little bloated, but its an approach someone else mentioned.
If you want to save a group of data for each cell, using an array of dictionaries is certainly a valid approach.
A dictionary is not suitable as the data source for a table view because, as the other poster said, dictionaries are unordered. A table view needs to be able to look up entries with a numeric index.
You could also create a custom data container object that had properties for the different items you need to store for each table view entry. You would then create an array of these data container objects. That would be a little cleaner (And probably a tad more efficient) than using an array of dictionaries. On the other hand, an array of dictionaries is very flexible. Need to store a new key/value pair for each table entry? Just add a new key to your dictionary.
Regards,
Duncan C WareTo
Animated GIF created with Face Dancer, available for free in the app store.
Replies
Also, I don't see you releasing editRecord anywhere, so you're leaking it.
EDIT: One more point, the convention is to name classes with a capital letter, so addUS should be AddUS.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI'm creating a new dictionary as I have problems passing a dictionary between methods (why I have that problem I'm still working on). I am [editRecord release] I just omitted it from the code.
Ultimately what I want to do is associate a key pair to a cell and use the ID I set against the disclosure button.
Do I need to create an array and put dictionaries in them? It seems a little bloated, but its an approach someone else mentioned.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBut if your cells require more then a simple array for their data, I suggest creating a container class for the data and using an array of those objects for the source.
For example, imagine you want to display, in each cell a persons: Name, Address, and in case a Phone number is available, show a disclosure button which would dial the number. You would start by creating a new class (subclass of NSObject) with three NSString properties (Name, Address, Phone). Your date source would then be an array of instances of this Person object, one object per cell. In the cellForRowAtIndex button, if a phone number is available you would show the disclosure. In accessoryButtonTappedForRowWithIndexPath you would grab the object with the indexPath.row and dial the number stored in that object.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeA dictionary is not suitable as the data source for a table view because, as the other poster said, dictionaries are unordered. A table view needs to be able to look up entries with a numeric index.
You could also create a custom data container object that had properties for the different items you need to store for each table view entry. You would then create an array of these data container objects. That would be a little cleaner (And probably a tad more efficient) than using an array of dictionaries. On the other hand, an array of dictionaries is very flexible. Need to store a new key/value pair for each table entry? Just add a new key to your dictionary.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome