English 中文(简体)
ICT - core data - table view scroll to取向
原标题:iOS - core data - table view scroll to newly added record

www.un.org/chinese/ecosoc

我有一个项目:

  • core data (using NSFetchedResultsController)
  • displaying the data in a table view (UITableView)

www.un.org/spanish/ecosoc 我想做的是:。

  • When a user adds a record, I want to scroll to the newly added record.

www.un.org/spanish/ecosoc 我做了些什么?

  1. When a new record is added, Inside the NSFetchedResultsControllerDelegate method, I store the index path in a property lastAddedIndexPath when the type is insert / update / move
  2. After invoking save, I scroll to the "lastAddedIndexPath"

<>Code>(NSFettorResultsControllerDelegate)

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{       
    if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
    {
        switch(type)
        {
            case NSFetchedResultsChangeInsert:
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                NSLog(@"going to store insert - scroll");
                self.lastAddedIndexPath = newIndexPath;
                break;

            case NSFetchedResultsChangeDelete:
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;

            case NSFetchedResultsChangeUpdate:
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                NSLog(@"going to store update - scroll");
                self.lastAddedIndexPath = newIndexPath;
                break;

            case NSFetchedResultsChangeMove:
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                NSLog(@"going to store move - scroll");
                self.lastAddedIndexPath = newIndexPath;
                break;
        }
    }
}

www.un.org/spanish/ecosoc 滚动守则

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    if (self.beganUpdates) //already [self.tableView beginUpdates] invoked
    {
        [self scrollToLastAddedIndexPath];  //contains the logic to scroll
        [self.tableView endUpdates];   
    }
}

<><>Problem>

  • I think the record is added to the table view asynchronously in a different thread.
  • so even after saving the database, when I scroll to the lastAddedIndexPath the record in the table doesn t exist yet.

<<>问题>

  1. How would I be able to scroll to the newly added record path after the record has been added to the table view ?
  2. Should I use notifications to know when the database is saved ?
  3. Is there any other alternate approach ?
最佳回答

Your scrolling has to happen after [self.tableView endUpdates] since the new row is not added to the table until that point. So switch the two statements around:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    if (self.beganUpdates) //already [self.tableView beginUpdates] invoked 
    { 
        [self.tableView endUpdates]; 
        [self scrollToLastAddedIndexPath];  //contains the logic to scroll 

    } 
} 
问题回答

您应:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

或者,在添加新记录时,你可以使用KVO通知。

在我的设想中,需要在表上显示最后增加的物体,这样就采用一种不同的方法,即使用NSSortDescriptor对阵列进行分类,然后每增加一次重新上载表格,以补充/取代任何记录。 ......

NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]autorelease];
NSArray * sortDescriptorArray = [[[NSArray alloc] initWithObjects:sortDescriptor,nil]autorelease];

self.commitsList =  [[self.listItem.itemCommit allObjects] sortedArrayUsingDescriptors:sortDescriptorArray];

[tblView reloadData];

And then you can use any key related to your data in initWithKey and sort your array to resolve this. And in CellForRowIndex method of table view you can use this array to populate tableview.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签