Showing posts with label UITableViewDataSource example. Show all posts
Showing posts with label UITableViewDataSource example. Show all posts

Saturday, June 8, 2013

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example in Objective C (iOS).


UITableViewDataSource tableView sectionForSectionIndexTitle atIndex

Asks the data source to return the index of the section having the given title and section title index.

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

Parameters of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
tableView
The table-view object requesting this information.
title
The title as displayed in the section index of tableView.
index
An index number identifying a section title in the array returned by sectionIndexTitlesForTableView:.

Return Value of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
An index number identifying a section.

Discussion of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
This method is passed the index number and title of an entry in the section index list and should return the index of the referenced section. To be clear, there are two index numbers in play here: an index to an section index title in the array returned by sectionIndexTitlesForTableView:, and an index to a section of the table view; the former is passed in, and the latter is returned. You implement this method only for table views with a section index list—which can only be table views created in the plain style (UITableViewStylePlain). Note that the array of section titles returned by sectionIndexTitlesForTableView: can have fewer items than the actual number of sections in the table view.

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[fetchedResultsController sections] count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

Example of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex].
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

// as the search icon is at index 0, scroll to the top
    if(index == 0)   
        [tableView scrollRectToVisible:CGRectMake(0, 0, searchBar.width, searchBar.height) animated:YES];   
    return index -1;
}

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    // Find the correct index of cell should scroll to.
    int foundIndex = 0;
    for (Object *obj in dataArray) {
        if ([[[obj.YOURNAME substringToIndex:1] uppercaseString] compare: title] == NSOrderedDescending)
            break;
        foundIndex++;
    }
    if(foundIndex > dataArray.count)
        foundIndex = dataArray.count;
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:foundIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    return 1;
}

End of UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example article.

UITableViewDataSource tableView moveRowAtIndexPath toIndexPath example in Objective C (iOS).


UITableViewDataSource tableView moveRowAtIndexPath toIndexPath

Tells the data source to move a row at a specific location in the table view to another location.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

Parameters
tableView
The table-view object requesting this action.
fromIndexPath
An index path locating the row to be moved in tableView.
toIndexPath
An index path locating the row in tableView that is the destination of the move.

Discussion of [UITableViewDataSource tableView moveRowAtIndexPath toIndexPath]
The UITableView object sends this message to the data source when the user presses the reorder control in fromRow.

UITableViewDataSource tableView moveRowAtIndexPath toIndexPath example.
In TableView Delegate method do following with your myStringArray (as TableView DataSource)

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 {  
   NSString *name = [[myStringsArray objectAtIndex:sourceIndexPath.row] retain];
   [myStringsArray removeObjectAtIndex:sourceIndexPath.row];
   [myStringsArray insertObject:name atIndex:destinationIndexPath.row];
   [name release];
 }
And Add two new Delegate in your .h file like this

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{   
  return YES;
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
   toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
  return proposedDestinationIndexPath;
}

Example of [UITableViewDataSource tableView moveRowAtIndexPath toIndexPath].
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSArray * fetchedObjects = [self.fetchedResultsController fetchedObjects];
    if (fetchedObjects == nil)
     return;

    NSUInteger fromRow = fromIndexPath.row + NUM_HEADER_SECTION_ROWS;
    NSUInteger toRow = toIndexPath.row + NUM_HEADER_SECTION_ROWS;

    NSInteger start = fromRow;
    NSInteger end = toRow;
    NSInteger i = 0;
    LinkObj *link = nil;

    if (toRow < start)
     start = toRow;
    if (fromRow > end)
     end = fromRow;

    @try {

     for (i = start; i <= end; i++) {
     link = [fetchedObjects objectAtIndex:i]; //
     //debug_NSLog(@"Before: %@", link);

     if (i == fromRow) // it's our initial cell, just set it to our final destination
     link.order = [NSNumber numberWithInt:(toRow-NUM_HEADER_SECTION_ROWS)];
     else if (fromRow < toRow)
     link.order = [NSNumber numberWithInt:(i-1-NUM_HEADER_SECTION_ROWS)]; // it moved forward, shift back
     else // if (fromIndexPath.row > toIndexPath.row)
     link.order = [NSNumber numberWithInt:(i+1-NUM_HEADER_SECTION_ROWS)]; // it moved backward, shift forward
     //debug_NSLog(@"After: %@", link);
     }
    }
    @catch (NSException * e) {
     debug_NSLog(@"Failure in moveRowAtIndexPath, name=%@ reason=%@", e.name, e.reason);
    }
}

UITableViewDataSource tableView moveRowAtIndexPath toIndexPath example.
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toIndexPath:(NSIndexPath *)destinationIndexPath;

  NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];

  // Grab the item we're moving.
  NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

  // Remove the object we're moving from the array.
  [things removeObject:thing];
  // Now re-insert it at the destination.
  [things insertObject:thing atIndex:[destinationIndexPath row]];

  // All of the objects are now in their correct order. Update each
  // object's displayOrder field by iterating through the array.
  int i = 0;
  for (NSManagedObject *mo in things)
  {
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
  }

  [things release], things = nil;

  [managedObjectContext save:nil];
}

End of UITableViewDataSource tableView moveRowAtIndexPath toIndexPath example article.