2014-02-09 30 views
2

獲取特定的細胞這是我的表視圖至今:點擊here如何在TableView中

正如你可以看到我在我的TableView 4項。我已經加載了一個Core Data數組。在我的Core數據實體中,我有一個名爲「Paid」的屬性,設置爲BOOLEAN默認值「NO」。我的表視圖鏈接到一個DetailViewController頁面,其中有一個名爲「Paid」的按鈕。當用戶點擊該按鈕時,該特定項目被標記爲「付費」。所以在我的cellForRowAtIndexPath我檢查是否該項目是否支付。通過圖片顯示我的代碼在這裏一定是錯誤的,因爲只有一個項目被標記爲付費。但是,當我NSLog其他項目在表格視圖中,他們也被標記爲「付費」。下面是我的cellForRowAtIndexPath方法的代碼:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];   

NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"OwedMoney"]; 
YouOweArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 
OwedMoney *YouOweObject = [YouOweArray objectAtIndex:indexPath.row]; 

if (YouOweObject.paid.boolValue == NO) 

{ 

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    [cell setBackgroundColor:[UIColor redColor]]; 
} 

else if (YouOweObject.paid.boolValue == YES) 
{ 

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    [cell setBackgroundColor:[UIColor greenColor]]; 
} 

在我DetailViewController我有一個「有償」按鈕,用下面的代碼:

- (IBAction)paid:(id)sender { 

    //Fetch Entity 

    NSManagedObjectContext *context = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"OwedMoney"]; 

    //Create an array that stores the contents of that entity 

    youOweArray = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    //Create managedObjectId 

    NSManagedObject *YouOweData = [youOweArray objectAtIndex:0]; 
    NSManagedObjectID *moID = [YouOweData objectID]; 

    //Successfully got the managedObjectId 

    NSLog(@"This is the id %@", moID); 

    BOOL Found = NO; 
    OwedMoney *Object; 


    for (OwedMoney *OwedObject in youOweArray) 
    { 
     NSManagedObjectID *OwedId = [OwedObject objectID]; 

     NSLog(@"%@", OwedObject); 
     if (OwedId == moID) 
     { 
      Found = YES; 
      Object = OwedObject; 
      break; 
     } 

    } 

    if (Found) 
    { 

     BOOL isPaid = Found; 
     Object.paid = [NSNumber numberWithBool:isPaid]; 

     NSLog(@"Is it paid: %@",Object.paid.boolValue? @"Yes":@"No"); 

     NSError *error = nil; 
     [self.managedObjectContext save:&error]; 
    } 
} 
+0

這是boolean類型 – doc92606

+0

的NSNumber的最有可能的 –

+0

我更新的代碼。如果只有我的第一個單元正確地響應代碼,而不是其他單元,那麼可能是什麼錯誤? – doc92606

回答

0

從主控制器以選擇對象的引用,並把在你的線上:

NSManagedObject *YouOweData = [youOweArray objectAtIndex:0]; 

並將其替換爲;

NSManagedObject *YouOweData = [youOweArray objectAtIndex:i]; 

檢查所選行中的mainController,你必須做出一個屬性DetailViewController:

@property (nonatomic) int selectedIndex; 

在MainController,你必須設置在didSelectRowAtIndexPath方法的屬性值。假設detailController是DetailViewController的對象。

設置爲:

detailController.selectedIndex = indexPath.row; 

在detailViewController:

NSManagedObject *YouOweData = [youOweArray objectAtIndex:selectedIndex]; 
+0

所以我是這樣的:NSIndexPath * i = [tableView indexPathForSelectedRow]; – doc92606

+0

但是這是在我的DetailViewController。我如何在我的DetailViewController中獲取我的TableViewController的選定對象? – doc92606

+0

例如我可以做NSIndexPath * i = [tableView indexPathForSelectedRow]但在我的DetailViewController中,我無法訪問我的tableView。我甚至不能[youOweArray ObjectAtIndex:indexPath.row]; – doc92606