views:

20

answers:

2

Hello, i want to populate a TableView with data from a database and, when tap a cell, in didSelectRowAtIndexPath, sending selected ID to a new View.

I would like to view, in cell text, the description of the record and to put ID in a hidden field/property of the cell, but i could not find a way to do it. I know i can get ID from the data source (something like [my_data_source objectAtIndex:indexPath.row]; ) but i don't really like this way, i would prefer to have ID assigned to a single cell.

Is there a way to do it?

Thanks in advance and greetings c.

+1  A: 

I'm guessing you've come from web development? I also found it difficult to do it this way, but its the best way. IT probably is possible - but its better if you get used to doing it like this, it really is.

Basically define an NSArray in the .h file (so the whole script can use it).

then in the init function:

// set the array
myArray = [NSArray arrayWithObjects:@"One",@"Two",@"Threee",nil];
[myArray retain];

then the table view delegate methods:

// set numebr of rows
- (CGFloat)tableView:(UITableView *)tableView numberOfRowsForSection:(NSUInteger)section {
  return [myArray count];
}

// set the cell titleLabel value
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // set the cell - I can't remember the exact code then do:
  cell.textLabel.text = [myArray objectAtIndex:[indexPath row]];
}

// similarly
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  NSLog(@"%@",[myArray objectAtIndex:[indexPath row]];
}

(disclaimer: these are just off the top of my head, and I haven't checked the exact methods - they're probably wrong in some way - but the code inside the functions is what you want really.)

When you've started using this you'll see its so much better than "hiding" an id somewhere in a table. To get things from the database I would suggest adding it all to a dictionary, or an array or similar and doing it like that when you init the class, but if you really want to do it dynamically then pretend your "hidden" ids are just index's of an array. So id#1 is at index 1 in your array. :)

Thomas Clayson
Thank you very much, i've followed your indications and it ran :-) As you said i come from web development and find it a little bit strange....
Cris
yeah, but soon you'll realise that if the web worked in the same way iphone worked the world would be a much better place! :p haha
Thomas Clayson
+1  A: 

Allright here is a quick hack on a different approach. I always like to deal with objects that are self contained. Pack all the data that you want into a custom class (here called MyData) for the table view you initialise it with the minimal amout that you need there. id and text that you pulled from the database. You also implement a function that can load the rest of the data from the DB.

When the item gets selected you pass the instance of your object to the subview controller and fill its data from the database. You can trigger the filling in the main viewcontroller or the subviewcontroller, that does not matter.

The main point is to pack all the data that goes together into one object (basically a "model" you already have a view and controller) and then fill views by accessing that object. This keeps your interface the same all the way through your applications. And makes changes easier. For example if you find out that it is better to fill in all the data from the DB at the start of your program you can do that now without changing the other views.

@interface MyObject : NSObject
{


}

// Create a stump object that contains only the necessary info
+ (id) withName:(NSString)name id:(int)id;

// loads the rest of your data from the DB
- (void) fillFromDb;

@property (readwrite, retain) NSString name;
@property (readwrite, assign) int id;
// The data fields that you need 

@end

// in tableview controller

@interface MyTableViewController ...
{
    NSMutableArray _dbData;

}


@end

@implementation MyTableViewController

- (void) viewDidLoad {
    // Load your data from DB
    for (int i =0; i < dbCount; ++i)
    {
        MyObject* data = [MyObject withName:dbName[i] id:dbId[i];
        [_dbData addObject:data];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  cell.textLabel.text = [_dbData objectAtIndex:[indexPath row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create Subviewcontroller

            // Optional call fillFromDb here and not in subviewcontroller
    subviewcontroller.dbData = [_dbData objectAtIndex:[indexPath row]];

    //activate subview
}


@interface SubViewController {
    MyObject* _dbData;
}

@end

@implementation SubViewController

- (void) viewDidLoad {
  [_dbData fillFromDb];
  // Do View Initialisations with the newly fetched Data
}

The code is here just to demonstrate the architecture

Harald Scheirich