在表视图控制器中设置整数值(didSelectRowAtIndexPath)用于PList(iOS)

时间:2011-11-28 21:54:01

标签: ios xcode if-statement uitableview didselectrowatindexpath

我试图使用PList来填充从视图控制器推出的视图控制器,该视图控制器是从表视图中推出的。更简单:

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

我试图这样做的方法是在用户点击表格视图中的行时将整数设置为一个值。然后我将使用该整数的值指向PList中的正确值并拉出相应的字符串。在第二个View Controller中,我试图用PList中的图像填充UIImageView。这是我的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];

所有内容都正确填充。我已尝试在表视图控制器(单击按钮)和DealsDetailViewController的viewDidLoad方法中NSLogDeDail.dealsInt的值。两次都返回值' 0' 0无论我点击哪一行。我的语法在哪里出错?

EDIT -------- 这是我退出时的结果:

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";
}

它会记录正确点击的对象的PList内容。所以它抓住了正确的信息(我认为这是因为它正确地填充了表格视图和详细视图......)

1 个答案:

答案 0 :(得分:2)

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

这似乎与您之前在dealsArray内使用该对象相矛盾。它看起来像上面一行中的字典:

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

然而,你在这里将它与静态字符串进行比较。发生的事情是,如果条件因此保持为零,则它不匹配任何这些。

您需要以下内容:

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

注意我从字典中获取另一个objectForKey(按照上面的编辑键为@“Name”),我也使用isEqualToString:这是比较字符串的正确方法。