Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
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.

Two object from one array, per UITableViewCell

djalfirevicdjalfirevic Posts: 88Registered Users
Hello,

I have an NSMutableDictionary which has key as a name, and UIImage as an object. In my view controller, I have a custom cell which displays image to the left and name to the right. Basically, something like UITableViewCellStyleDefault.

When I have one image and one name per row, everything is ok. Now I want to have two images and two names, per one cell, but from that one dictionary.

How to do that?
Post edited by djalfirevic on
And thus I clothe my naked villainy; with old odd ends stolen forth from holy writ; and seem a saint when most I play the devil.

Replies

  • DomeleDomele Posts: 2,948Registered Users
    I would make a custom object, unless you want to make an array within a dictionary but for what you are doing, that is pretty sloppy.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • matt62kingmatt62king Posts: 78Registered Users
    You can make an NSArray of NSDictionary Objects, from there it is simple to use in a UITableView. It should look something like this


    - (UITableViewCell)cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *dictionary = (NSDictionary *)[someArray objectAtIndex:indexPath.row];

    // Code to build the cell......

    someCell.someLabel.text = [dictionary objectForKey:@\"textKey\"];
    someCell.someImageView.image = [dictionary objectFroKey:@\"imageKey\"];

    return someCell;
    }
  • djalfirevicdjalfirevic Posts: 88Registered Users
    This snippet is fine, but it's not what I want to do. I want something like this:


    - (UITableViewCell)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = (NSDictionary *)[array objectAtIndex:indexPath.row];
    NSDictionary *dict1 = (NSDictionary *)[array objectAtIndex:indexPath.row + 1];

    // Code to build cell

    someCell.someLabel.text = [dict objectForKey:@\"textKey\"];
    someCell.someImageView.image = [dict objectFroKey:@\"imageKey\"];
    someCell.someLabel1.text = [dict1 objectForKey:@\"textKey\"];
    someCell.someImageView1.image = [dict1 objectFroKey:@\"imageKey\"];

    return someCell;
    }


    Do you now know what I want to do? I want to take two objects in one iteration, and store objects/keys to one cell, etc. Of course, if I have 17 object/key pairs, I want to store them in 9 rows. 8 rows would have 2 pics and 2 labels, and the last would have 1 image and 1 label, and other image/label would be empty.
    And thus I clothe my naked villainy; with old odd ends stolen forth from holy writ; and seem a saint when most I play the devil.
  • Duncan CDuncan C Posts: 8,019Tutorial Authors, Registered Users
    djalfirevic;320079 said:
    This snippet is fine, but it's not what I want to do. I want something like this:


    - (UITableViewCell)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = (NSDictionary *)[array objectAtIndex:indexPath.row];
    NSDictionary *dict1 = (NSDictionary *)[array objectAtIndex:indexPath.row + 1];

    // Code to build cell

    someCell.someLabel.text = [dict objectForKey:@\"textKey\"];
    someCell.someImageView.image = [dict objectFroKey:@\"imageKey\"];
    someCell.someLabel1.text = [dict1 objectForKey:@\"textKey\"];
    someCell.someImageView1.image = [dict1 objectFroKey:@\"imageKey\"];

    return someCell;
    }


    Do you now know what I want to do? I want to take two objects in one iteration, and store objects/keys to one cell, etc. Of course, if I have 17 object/key pairs, I want to store them in 9 rows. 8 rows would have 2 pics and 2 labels, and the last would have 1 image and 1 label, and other image/label would be empty.

    The other poster has a much better idea. Save both text strings and both images in a single dictionary. Save that dictionary in an array at the indexPath.row. (Or create an array of custom data objects where the custom data object has properties for all the attributes you need for a cell in your table view.)

    You COULD use two array indexes for each cell, but you're going to drive yourself crazy doing it. You would need to use code like this:

    int dict1Index =indexPath.row*2;
    int dict2Index =indexPath.row*2+1;

    NSDictionary *dict = (NSDictionary *)[array objectAtIndex: dict1Index];
    NSDictionary *dict1 = (NSDictionary *)[array objectAtIndex: dict2Index];


    You would then need to make sure that you create and maintain 2 array entries for each REAL entry in your table view.

    Again, I strongly advise against this approach. You're going to confuse yourself, cause problems that are hard to debug, and confuse the hell out of anybody else who tries to look at/maintain your code.

    EDIT: Even worse, you will have code that is completely inflexible. If you later need to add a third text field, but not a third picture, how would you do that? What about a checkbox, slider, or segmented control? Are you going to go back and change your data structures to save THREE data elements per table view cell? What if you only want to add one more thing, not two/three? it's just a messy, ugly way to store your data. Bad design, bad idea. Don't do it.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.