2013-02-18 53 views
0

所以我有5行和选择他们传递一个整数与行号到我的第二个视图控制器。即使它识别输入,TableView不会更新数据?

每个数字都有其自己的数组,并且应该返回指定行的数量。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MasterCell"; 

    MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 


if (radInt == 0) { 
    cell.textLabel.text = @"LM"; 
    NSLog(@"My return should have been LM"); 

} 
if (radInt == 1) { 
    cell.textLabel.text = @"Restauranger"; 
    NSLog(@"My return should have been Rest"); 
} 
    if (radInt == 2) { 
     cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row]; 
    } 
    if (radInt == 3) { 
     cell.textLabel.text = [annatArray objectAtIndex:indexPath.row]; 
    } 

    //cell.textLabel.text = [listData objectAtIndex:[indexPath row]]; 
    cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 


    return cell; 
} 

这是我的代码只是为了测试出来,这是行不通的。 NSLOG工作正常,但数据只是更新...我做错了什么?它每次都记录LM,但它在选定行(radInt)的日志中也有1个记录。

新方法

static NSString *CellIdentifier = @"MasterCell"; 
    MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) 
     cell = [[MasterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    if (radInt == 0) { 
     cell.textLabel.text = @"LM"; 
     NSLog(@"My return should have been LM"); 

    } 
    else if (radInt == 1) { 
     cell.textLabel.text = @"Restauranger"; 
     NSLog(@"My return should have been Rest"); 
    } 
    else if (radInt == 2) { 
     cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row]; 
    } 
    else if (radInt == 3) { 
     cell.textLabel.text = [annatArray objectAtIndex:indexPath.row]; 
    } 

    else { 
     cell.textLabel.text = @"Noes"; 
    } 


    //cell.textLabel.text = [listData objectAtIndex:[indexPath row]]; 
    cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    return cell; 

之前的观点----

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SecondViewController *enjoy = [[SecondViewController alloc] init]; 
    if ([[array objectAtIndex:indexPath.row] isEqual:@"Livsmedel"]) { 
     [enjoy setTitle:[array objectAtIndex:indexPath.row]]; 
     enjoy.radInt = 0; 
     NSLog(@"0"); 
    } 

    if ([[array objectAtIndex:indexPath.row] isEqual:@"Restauranger"]) { 
     [enjoy setTitle:[array objectAtIndex:indexPath.row]]; 
     enjoy.radInt = 1; 
     NSLog(@"1"); 
    } 

    if ([[array objectAtIndex:indexPath.row] isEqual:@"Shopping"]) { 
     [enjoy setTitle:[array objectAtIndex:indexPath.row]]; 
     enjoy.radInt = 2; 

    } 
    if ([[array objectAtIndex:indexPath.row] isEqual:@"Annat"]) { 
     enjoy.radInt = 3; 
    } 

    [self performSegueWithIdentifier:@"main" sender:self]; 
} 
+0

看起来'indexPath'和你的选择变量('radInt')之间没有连接。 – Matthias 2013-02-18 13:46:40

+0

if(cell == nil)?? – Omarj 2013-02-18 14:55:55

+0

你记录了一下,看看它是什么吗?你如何将它传递给这个表视图? – rdelmar 2013-02-18 16:22:36

回答

0

你实际上并不需要实现didSelectRowAtIndexPath方法:如果你在故事板从小区到连接SEGUE下个控制器。你需要实现的是prepareForSegue :.你的代码应该是这样的:

- (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender 
{ 
    NSInteger row = [self.tableView indexPathForSelectedRow].row; 
    SecondViewController *enjoy = segue.destinationViewController; 
    if ([[array objectAtIndex:row] isEqual:@"Livsmedel"]) { 
     [enjoy setTitle:[array objectAtIndex:row]]; 
     enjoy.radInt = 0; 
     NSLog(@"0"); 
    } 

    if ([[array objectAtIndex:row] isEqual:@"Restauranger"]) { 
     [enjoy setTitle:[array objectAtIndex:row]]; 
     enjoy.radInt = 1; 
     NSLog(@"1"); 
    } 

    if ([[array objectAtIndex:row] isEqual:@"Shopping"]) { 
     [enjoy setTitle:[array objectAtIndex:row]]; 
     enjoy.radInt = 2; 

    } 
    if ([[array objectAtIndex:row] isEqual:@"Annat"]) { 
     enjoy.radInt = 3; 
    } 
} 
+0

非常感谢:D确实有效。我不知道你可以用prepareForSegue来发布故事板中的数据。非常感谢! – 2013-02-18 20:14:59