سؤال

I'm attempting to use a PList to populate a view controller that's pushed from a view controller that's pushed from a table view. More simply:

TableViewController-->DetailViewController-->2ndDetailViewController(<-This one is the one I'm populating)

The way I'm attempting to do this is by setting an integer to a value when the user clicks on the row in the Table View. I will then use the value of that integer to point to the right value in the PList and pull out the corresponding string. In the second View Controller, I'm trying to populate a UIImageView with an image from the PList. Here's the code of my didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     DealsDetailViewController *dealsDetail = [[DealsDetailViewController alloc]initWithNibName:@"DealsDetailViewController" bundle:nil];
     dealsDetail.dealsImageString = [[self.dealsArray objectAtIndex:indexPath.row] objectForKey:@"Image"];
     dealsDetail.dealsLabelString = [[self.dealsArray objectAtIndex:indexPath.row] objectForKey:@"Description"];
     dealsDetail.title = [[self.dealsArray objectAtIndex:indexPath.row] objectForKey:@"Name"];     

if ([[dealsArray objectAtIndex:indexPath.row] isEqual:@"Deal #1"]) {
    dealsDetail.dealsInt = 0;
    [dealsDetail setTitle:[dealsArray objectAtIndex:indexPath.row]];
}

if ([[dealsArray objectAtIndex:indexPath.row] isEqual:@"Deal #2"]) {
    dealsDetail.dealsInt = 1;
    [dealsDetail setTitle:[dealsArray objectAtIndex:indexPath.row]];
}

if ([[dealsArray objectAtIndex:indexPath.row] isEqual:@"Deal #3"]) {
    dealsDetail.dealsInt = 2;
    [dealsDetail setTitle:[dealsArray objectAtIndex:indexPath.row]];
}

[self.navigationController pushViewController:dealsDetail animated:YES];
[dealsDetail release];

Everything gets populated correctly. I've attempted to NSLog the value of dealsDetail.dealsInt in both the table view controller (on clicking the button) and in the viewDidLoad method of the DealsDetailViewController. Both times, it returns a value of '0' no matter which row I click. Where have I gone wrong in my syntax?

EDIT-------- Here's what I get when I log out:

NSLog(@"%@",[dealsArray objectAtIndex:indexPath.row]);


2011-11-28 17:40:59.092 Stores Tab Bar[7386:207] {
    Barcode = "SampleBarCode3.png";
    Description = "Here's the deal 3";
    Image = "FritoTest.png";
    Name = "Deal #3";
    Thumbnail = "dollars.png";
}

It logs out the PList contents of the object that's clicked correctly. So it's grabbing the right information (which I presumed it was since it was populating the table view and the detail view correctly...)

هل كانت مفيدة؟

المحلول

if ([[dealsArray objectAtIndex:indexPath.row] isEqual:@"Deal #1"]) {
    dealsDetail.dealsInt = 0;
    [dealsDetail setTitle:[dealsArray objectAtIndex:indexPath.row]];
}

This seems to contradict your earlier use of the object within dealsArray. It looks like a dictionary in the lines above:

dealsDetail.dealsImageString = [[self.dealsArray objectAtIndex:indexPath.row] objectForKey:@"Image"];

And yet here you are comparing it to a static string. What is happening is that it isn't matching any of these if conditions and so is remaining at zero.

You want the following:

if ([[[dealsArray objectAtIndex:indexPath.row] objectForKey:@"Name"] isEqualToString:@"Deal #1"]) {

Note I am taking another objectForKey (key being @"Name" as per your edit above) from the dictionary, and I am also using isEqualToString: which is the correct way of comparing strings.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top