I want to refresh tagListTableView.
but it doesn't refresh.
When delete button on UITableview cell is tapped , object in coraData's date could be removed.
but, Deleting a cell in a table view is still exposed to the screen to blank.
I tried to [tagListTableView reloadData] on viewWillAppear, viewDidAppear, .....more.
What am I missing??
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[tagListTableView reloadData];
[pickedTags removeObject:tag];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//********
//REMOVE the object before reloading the table
[pickedTags removeObject:tag];
[tagListTableView reloadData];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
You should remove object from your pickedTags before you reload the tableview, you were doing is after reload.
It might help you.
Related
I have the exact same problem as the question posted here.
Which looks exactly like this gif.
I'm not which method to begin troubleshooting the problem. I've sub-classed the UICell and have used the standard UICell, both with the same results. I've also tried to set the image in the cell to nil when it edit mode: cell.imageView.image = nil;
Any thoughts would be appreciated, I can post more code if needed...
// Cell For Row At Index Path
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Item cell func");
static NSString *CellIdentifier = #"detailCell";
MCTableViewCell *cell = nil;
if (cell == nil)
{
cell = [[MCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Item * record = [_itemsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageWithData:record.photo];
cell.textLabel.text = [NSString stringWithFormat:#" %#", record.itemName];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
//cell.shouldIndentWhileEditing = NO;
NSLog(#"Detail Record.Name %#", [_itemsArray objectAtIndex:indexPath.row]);
return cell;
}
// Editing Cells
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Begin editStyle Func");
if (editingStyle == UITableViewCellEditingStyleDelete) {
//UITableViewCellEditingStyleNone
// 1
[tableView beginUpdates];
// Delete the row from the data source
NSLog(#"after Updates editStyle Func");
// Delete item from Table
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"after Row Animation editStyle Func");
// Delete Item from Database
[self.managedObjectContext deleteObject:[self.itemsArray objectAtIndex:indexPath.row]];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Save: %#", [error localizedDescription]);
}
[self showObjects];
NSLog(#"End of if stmnt");
// 5
[tableView endUpdates];
}
NSLog(#"End of Function");
}
This problem seems to happen in ios7.
in ios8, works fine. so, this might be a bug.
To work around, add your own UIImageView to cell and use it instead of UITableVewCell's imageView.
i'm trying to delete an object in core data when you press swipe and press delete. The problem is it deletes the the tablecell, but when i go back and in again the deleted cells are back again. I guess thats because i only deleted the object in the NSMUtableArray (devices) and did not delete the core data object. How can i do this?
The saveTap where the objects are saved:
-(void)saveTap:(id)sender{
Entity *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:#"Entity" inManagedObjectContext:_managedObjectContext];
[newManagedObject setValue:self.textfield.text forKey:#"playlistName"];
[newManagedObject setValue:[NSNumber numberWithInt:selectedRowValue] forKey:#"idnumber"];
// Save the context.
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self fetchDevices];
NSLog(#"COUNT %d", [devices count]);
}
and the commiteditingstyle method
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}
Is [self.devices objectAtIndex:indexPath.row]; returning that NSManagedObject you want to delete?
Then you should make the second function like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_managedObjectContext deleteObject:[self.devices objectAtIndex:indexPath.row]];
[_managedObjectContext save:nil];
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}
Instead of reloading while of UitableView you should only reloadrowatindexpaths: and before it [UITableView beganEditing];
And after Reloading rows
[UITableView endEditing];
Hello I'm sorry I'm a beginner. I have a table row values in UILabel that are loaded with Core Data and I need to somehow compare and find the greatest value and thus change the background color to red UILabel. I'm very happy for each answer.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.voda.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *vodapocet = [self.voda objectAtIndex:indexPath.row];
UILabel *dLabel= (UILabel *)[cell viewWithTag:10];
[dLabel setText:[NSString stringWithFormat:#"%#" ,[vodapocet valueForKey:#"datum"]]];
UILabel *sLabel= (UILabel *)[cell viewWithTag:20];
[sLabel setText:[NSString stringWithFormat:#"%#" ,[vodapocet valueForKey:#"spotreba" ]]];
[sLabel setBackgroundColor:[UIColor greenColor]];
UILabel *cLabel= (UILabel *)[cell viewWithTag:30];
[cLabel setText:[NSString stringWithFormat:#"%#" ,[vodapocet valueForKey:#"cena"]]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.voda objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.voda removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Get the maximum value with:
NSNumber *maxSpotreba = [self.voda valueForKeyPath:#"#max.spotreba"];
Then in cellForRowAtIndexPath You can compare this number with the number for the current row to determine what to do.
I want to implement a UITableView using core data and when I press a Trash button in my ViewController, I can see all the delete buttons on the right of all the cells without the swipe gesture. But When i enabled the editing mode only Delete Button should have to be displayed on the right side
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.notes objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
}
// Remove note from table view
[self.notes removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
can anyone suggest me how it is possible
Edited :
Write Following Code :
-(void) yourMethodName:(UIButton *) sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[self.tblView setEditing:NO animated:NO];
[self.tblView reloadData];
}
else
{
[super setEditing:YES animated:YES];
[self.tblView setEditing:YES animated:YES];
[self.tblView reloadData];
}
}
Here is my method in my delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
PFRelation *friendsRelation = [self.currentUser relationforKey:#"friendsRelation"];
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryNone;
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user];
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.friends addObject:user];
[friendsRelation addObject:user];
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog (#"Error %# %#", error, [error userInfo]);
}
}];
}
When I run the app, everything works just fine with no errors or warnings but when I tap on friends to remove the checkmark by their name it does not happen.
This code syntax might help you check that it's working. Then you'll probably see as Matthias said that the issue is coming from your isFriend: method because your code looks fine for me.
Or another way, I think it's cleaner to separate the two logics of selecting/deselecting, so don't forget to enable the "multiple selection" on the tableview :
self.tableView.allowsMultipleSelection = YES;
And on the tableview delegate :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
Then to retrieve all selected items :
- (void)handleSelectedItems
{
NSArray *selectedItemIndexPaths = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *indexPath in selectedItemIndexPaths) {
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
// do what you want
// ...
}
}