2013-02-18 23 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