Ranjit
Ranjit

Reputation: 4636

Move rows to bottom in a TableView which uses CoreData

In my app I have a folder-file (one-to-many relationship).So when I open my app, my firstViewController contains all files and when clicked on each folder, it opens a tableViewController displaying files in that folder.

Now here whenever I swipe any row, it is moved to bottom row and the table is update accordingly, the next swiped row will go and sit above the first swiped row.

Eg, at present if my table contains

0 1 2 3
A B C D

After swiping A

0 1 2   3
B C D **A**

after swiping B

0 1   2 3
C D **B A**

and so on,

To do this functionality, I should update the NSOrderedSet also which is shown below

for (int i = 0; i < [self.folder.file count]; i++)
        {
            File *file =  [self.folder.file objectAtIndex:i];
            if(file.completedStatus == false)
            {
                m_position = i;
                m_finalIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
            }
        }

        file.completedStatus = YES;

        id obj = [self.folder. file objectAtIndex:row];

        NSMutableOrderedSet *files = [self.folder.file mutableCopy];
        [files removeObjectAtIndex:row];
        [files insertObject:obj atIndex:m_position];


        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
        [m_tableView moveRowAtIndexPath:currentIndexPath toIndexPath:m_finalIndexPath];         
        self.folder.file = files;       
         }
         [self.managedObjectContext save:nil];    
         [m_tableView reloadData];

This works fine in this View, and here self.folder, is particular folder on which the user clicks

Now I have another ViewController called TestViewController where I have some of the file objects irrespective of in which ever folder they are present . I have populated the table of this TestViewController with these files, Now here also, when I swipe the cells, they will change the color but not move their position as above

Eg

0 1 2 3
A C B D

after swiping A and B

  0   1   2   3
**A** C **B** D

Now here A can belong to folder 1 and B can belong to folder 2,

when I close(dismiss) this TestViewController, What should happen is that

a) it shows me the fileViewController by default b) the color of file A should change and it should move to a position by checking whether there are any swiped cells already. If yes, it should be placed above it or else at the bottom.

c) similarly for color B.

Regards Ranjit.

Upvotes: 1

Views: 245

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

Ranjit,

About your question I cannot understand what you are asking here. So, please provide some other details.

To know the folder a specific file belongs to, this is quite simple to achieve.

If you have set up an inverse relationship between a file and a folder, simply access that relationship.

For example, if the inverse relationship is called toFolder, you can retrieve the current folder like

Folder* currentFolder = currentFile.toFolder; // or access it through KVC

To create an inverse relationship take a look here: Core data structure use multiple entities or not?

Upvotes: 2

Related Questions