Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-15-2011, 03:25 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 35
scrumsolutions is on a distinguished road
Question Using dictionary instead of array for disclosure button

Hi All,

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;

Code:
[[self contentsList] objectAtIndex:[indexPath row]]
The code of trouble is;

Code:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *test = [NSDictionary dictionaryWithObjectsAndKeys:@"User 1", @"1", @"User  2", @"2", @"User 3", @"8", @"User 4", @"4", @"User 5", @"5", nil];
    [self setContentsListDic:test];
    addUS *editRecord = [[addUS alloc] init];
    NSEnumerator *enumerator = [contentsListDic keyEnumerator];
    id key;
    key = [enumerator nextObject];
    editRecord.editID = key;
    [self presentModalViewController: editRecord animated:YES];
scrumsolutions is offline   Reply With Quote
Old 06-15-2011, 03:36 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 06-15-2011, 03:51 PM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 35
scrumsolutions is on a distinguished road
Wink Playground project :-)

Quote:
Originally Posted by baja_yu View Post
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.
scrumsolutions is offline   Reply With Quote
Old 06-15-2011, 04:14 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 06-15-2011, 04:15 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by scrumsolutions View Post
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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
array, dictionary, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
6 members and 311 guests
chemistry, Dnnake, iOS.Lover, lendo, Leslie80, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0