獲取特定的細胞這是我的表視圖至今:點擊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];
}
}
這是boolean類型 – doc92606
的NSNumber的最有可能的 –
我更新的代碼。如果只有我的第一個單元正確地響應代碼,而不是其他單元,那麼可能是什麼錯誤? – doc92606