Domanda

I have a splitview app, a table with customers to the left and their info on the right so i have to pass witch customer that is clicked and i can't use the name in case of two have the same so im trying to pass over the customer id in the tag property of the cell but i dont get it to work.

Here is where i set the value, i can see it's working when debugging

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

Customer names
NSDate *object = _objects[indexPath.row];

//Customer number
NSString *nr = _kundnr[indexPath.row];

//Customer name
cell.textLabel.text = [object description];

//Customer number
NSInteger Ftgnr = [nr intValue];

//Setting the customer number to cell tag
cell.tag = Ftgnr;

return cell;
}

Here i pick up the cell.tag it's the detailview viewdidload, i tried many different ways to save the id to a variable but the app crashes and i don't know if the value gets passed. It's working to pick up the description (customer name) but i want the id.

- (void)configureView
{
// Update the user interface for the detail item.

if (self.detailItem) {

  //  NSString *strCellTag = [NSString stringWithFormat:@"%d", [self.detailItem tag]];

   //here im trying to pick up the tag value this is where the app craches
   NSInteger Ftgnr = [self.detailItem tag];

   // NSString *stringformat = [NSString stringWithFormat:@"%d", Ftgnr];

    //Its working to pick up the text from the cell
    self.detailDescriptionLabel.text = [self.detailItem description];
}

Thank you for your help!

È stato utile?

Soluzione

Add a property to your detail view controller so you can set the id when you set the detail item. Then:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailViewController.detailIdentity = _kundnr[indexPath.row];
    self.detailViewController.detailItem = _objects[indexPath.row];
}

And then use self.detailIdentity instead of [self.detailItem tag]. And, it's already a string so you don't need to convert to integer and back.

You should probably also:

  1. Rename detailItem to be more specific, like detailDate
  2. Think about creating a custom class to hold the full detailItem (so it contains the identity and the date)

Altri suggerimenti

My final code

Masterviewcontroller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];

NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *object = _objects[indexPath.row];
NSDate *kundnr = _kundnr[indexPath.row];
self.detailViewController.detailItem = object;
self.detailViewController.detailIdentity = kundnr;

}

Detailviewcontroller

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
    _detailItem = newDetailItem;

    // Update the view.
    [self configureView];
}

if (self.masterPopoverController != nil) {
    [self.masterPopoverController dismissPopoverAnimated:YES];
}        
}

- (void)setDetailIdentity:(id)newDetailItentity
{
if (_detailIdentity != newDetailItentity) {
    _detailIdentity = newDetailItentity;

    // Update the view.
    [self configureView];
}

if (self.masterPopoverController != nil) {
    [self.masterPopoverController dismissPopoverAnimated:YES];
}
}

- (void)configureView
{
// Update the user interface for the detail item.

if (self.detailItem) {

   // NSString *stringformat = [NSString stringWithFormat:@"%d", Ftgnr];
    self.detailDescriptionLabel.text = [self.detailItem description];
}


if (self.detailIdentity) {

    NSString *kundnr = [self.detailIdentity description];

}

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top