Showing posts with label UITableView. Show all posts
Showing posts with label UITableView. Show all posts

Saturday, June 8, 2013

UITableView tableView willSelectRowAtIndexPath example in Objective C (iOS).


UITableView tableView willSelectRowAtIndexPath

Tells the delegate that a specified row is about to be selected.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView willSelectRowAtIndexPath]
tableView
A table-view object informing the delegate about the impending selection.
indexPath
An index path locating the row in tableView.

Return Value of [UITableView tableView willSelectRowAtIndexPath]
An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don'€™t want the row selected.

Discussion of [UITableView tableView willSelectRowAtIndexPath]
This method is not called until users touch a row and then lift their finger; the row isn'€™t selected until then, although it is highlighted on touch-down. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down. This method isn’t called when the table view is in editing mode (that is, the editing property of the table view is set to YES) unless the table view allows selection during editing (that is, the allowsSelectionDuringEditing property of the table view is set to YES).

UITableView tableView willSelectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( indexPath.row =< 2 ) return nil;

    // By default, allow row to be selected
    return indexPath;
}

Example of [UITableView tableView willSelectRowAtIndexPath].
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
    // Determine if row is selectable based on the NSIndexPath.

    if (rowIsSelectable)
    {
        return path;
    }

    return nil;
}

UITableView tableView willSelectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CommentViewController *commentViewController = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
        Comment *selectedComment = [[[Comment alloc] init] retain];
        selectedComment = [self.message.comments objectAtIndex:indexPath.row];
        commentViewController.comment = selectedComment;

        [self presentModalViewController:commentViewController animated:YES];

        [selectedComment release];
        [commentViewController release];   
}

End of UITableView tableView willSelectRowAtIndexPath example article.

UITableView tableView willDisplayHeaderView forSection example in Objective C (iOS).


UITableView tableView willDisplayHeaderView forSection

Tells the delegate that a header view is about to be displayed for the specified section.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

Parameters of [UITableView tableView willDisplayHeaderView forSection]
tableView
The table-view object informing the delegate of this event.
view
The header view that is about to be displayed.
section
An index number identifying a section of tableView.

UITableView tableView willDisplayHeaderView forSection example.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if(section == 0)
        return @"Countries to visit";
    else
        return @"Countries visited";
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    //displays the header of the tableView
    self.tableView.tableHeaderView = view;

}

Example of [UITableView tableView willDisplayHeaderView forSection].
- (void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    DLog(@"0>>");
    CGRect originalRect = view.frame;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow_top.png"]];
    [imageView setFrame:CGRectMake(0.0f, originalRect.size.height - imageView.frame.size.height, imageView.frame.size.width, imageView.frame.size.height)];
    [view addSubview:imageView];
}

- (void) tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
     DLog(@"0>f>");
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow_bottom.png"]];
    [imageView setFrame:CGRectMake(0.0f, 0.0f, imageView.frame.size.width, imageView.frame.size.height)];
    [view addSubview:imageView];
}

End of UITableView tableView willDisplayHeaderView forSection example article.

UITableView tableView willDisplayFooterView forSection example in Objective C (iOS).


UITableView tableView willDisplayFooterView forSection

Tells the delegate that a footer view is about to be displayed for the specified section.

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section

Parameters of [UITableView tableView willDisplayFooterView forSection]
tableView
The table-view object informing the delegate of this event.
view
The footer view that is about to be displayed.
section
An index number identifying a section of tableView .

UITableView tableView willDisplayFooterView forSection example.
- (void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    DLog(@"0>>");
    CGRect originalRect = view.frame;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow_top.png"]];
    [imageView setFrame:CGRectMake(0.0f, originalRect.size.height - imageView.frame.size.height, imageView.frame.size.width, imageView.frame.size.height)];
    [view addSubview:imageView];
}

- (void) tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
     DLog(@"0>f>");
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow_bottom.png"]];
    [imageView setFrame:CGRectMake(0.0f, 0.0f, imageView.frame.size.width, imageView.frame.size.height)];
    [view addSubview:imageView];
}

End of UITableView tableView willDisplayFooterView forSection example article.

UITableView tableView willDisplayCell forRowAtIndexPath example in Objective C (iOS).


UITableView tableView willDisplayCell forRowAtIndexPath

Tells the delegate the table view is about to draw a cell for a particular row.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView willDisplayCell forRowAtIndexPath]
tableView
The table-view object informing the delegate of this impending event.
cell
A table-view cell object that tableView is going to use when drawing the row.
indexPath
An index path locating the row in tableView.

Discussion of [UITableView tableView willDisplayCell forRowAtIndexPath]
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

UITableView tableView willDisplayCell forRowAtIndexPath example.
So: If you add this method to your table view delegate:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = cell.contentView.backgroundColor;
}
Then in your cellForRowAtIndexPath method you can do:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

Example of [UITableView tableView willDisplayCell forRowAtIndexPath].

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:255.0/255 green:255.0/255 blue:145.0/255 alpha:1] : [UIColor clearColor];
  cell.backgroundColor = color;
}

UITableView tableView willDisplayCell forRowAtIndexPath example.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ((indexPath.row % 2) == 1) {
        cell.backgroundColor = UIColorFromRGB(0xEDEDED);
        cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED);
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

}

End of UITableView tableView willDisplayCell forRowAtIndexPath example article.

UITableView tableView willBeginEditingRowAtIndexPath example in Objective C (iOS).


UITableView tableView willBeginEditingRowAtIndexPath

Tells the delegate that the table view is about to go into editing mode.

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView willBeginEditingRowAtIndexPath]
tableView
The table-view object providing this information.
indexPath
An index path locating the row in tableView.

Discussion of [UITableView tableView willBeginEditingRowAtIndexPath]
This method is called when the user swipes horizontally across a row; as a consequence, the table view sets its editing property to YES (thereby entering editing mode) and displays a Delete button in the row identified by indexPath. In this "€œswipe to delete"€ mode the table view does not display any insertion, deletion, and reordering controls. This method gives the delegate an opportunity to adjust the application'€™s user interface to editing mode. When the table exits editing mode (for example, the user taps the Delete button), the table view calls tableView:didEndEditingRowAtIndexPath:.

Note: A swipe motion across a cell does not cause the display of a Delete button unless the table view'€™s data source implements the tableView:commitEditingStyle:forRowAtIndexPath: method.

UITableView tableView willBeginEditingRowAtIndexPath example.
- (void)tableView:(UITableView *)aTableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
   [super setEditing:isEditing animated:animated];
   [self.tableView setEditing:isEditing animated:animated];

   //Self.editing handles the done / edit button
   self.editing = YES;
}

Example of [UITableView tableView willBeginEditingRowAtIndexPath].
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"tableView canEditRowAt...");
    return YES;
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"tableView willBeginEditingRowAtIndexPath: %@", indexPath);
}

- (void)tableView:(UITableView *)tableView willEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"tableView willEndEditingRowAtIndexPath: %@", indexPath);
}

UITableView tableView willBeginEditingRowAtIndexPath example.
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{
 NSLog(@"WILL BEGIN EDITING");

 [self.tableView setEditing:YES animated:YES];

}

-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

[self.tableView setEditing:NO animated:YES];

}

End of UITableView tableView willBeginEditingRowAtIndexPath example article.

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example in Objective C (iOS).


UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath

Asks the delegate to return a new index path to retarget a proposed move of a row.

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

Parametersof [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
tableView
The table-view object that is requesting this information.
sourceIndexPath
An index-path object identifying the original location of a row (in its section) that is being dragged.
proposedDestinationIndexPath
An index-path object identifying the currently proposed destination of the row being dragged.

Return Value of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
An index-path object locating the desired row destination for the move operation. Return proposedDestinationIndexPath if that location is suitable.

Discussion of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
This method allows customization of the target row for a particular row as it is being moved up and down a table view. As the dragged row hovers over another row, the destination row slides downward to visually make room for the relocation; this is the location identified by proposedDestinationIndexPath.

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
  if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
    NSInteger row = 0;
    if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
      row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
    }
    return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];    
  }

  return proposedDestinationIndexPath;
}

Example of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath].
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if( sourceIndexPath.section != proposedDestinationIndexPath.section )
    {
        return sourceIndexPath;
    }
    else
    {
        return proposedDestinationIndexPath;
    }
}

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:sourceIndexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"Moving Cell to %d %d", proposedDestinationIndexPath.section, proposedDestinationIndexPath.row];
    return proposedDestinationIndexPath;
}

End of UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example article.

UITableView tableView shouldShowMenuForRowAtIndexPath example in Objective C (iOS).


UITableView tableView shouldShowMenuForRowAtIndexPath

Asks the delegate if the editing menu should be shown for a certain row.

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView shouldShowMenuForRowAtIndexPath]
tableView
The table-view object that is making this request.
indexPath
An index-path object locating the row in its section.

Return Value
YES if the editing menu should be shown positioned near the row and pointing to it, otherwise NO. The default value is NO.

Discussion of [UITableView tableView shouldShowMenuForRowAtIndexPath]
If the user tap-holds a certain row in the table view, this method (if implemented) is invoked first. Return NO if the editing menu shouldn’t be shown—for example, the cell corresponding to the row contains content that shouldn’t be copied or pasted over.

UITableView tableView shouldShowMenuForRowAtIndexPath example.
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    return (action == @selector(copy:));
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:))
        NSLog(@"in real life, we'd now copy somehow");
}

Example of [UITableView tableView shouldShowMenuForRowAtIndexPath].
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return (action == @selector(copy:));
}

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}

UITableView tableView shouldShowMenuForRowAtIndexPath example.
Instead, you can use this delegate method:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
To hide the standard items (cut, copy, and paste), return NO here:

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}
Then you need to return YES from canBecomeFirstResponder like you have and, for some reason, I had to implement this method too:

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}

End of UITableView tableView shouldShowMenuForRowAtIndexPath example article.

UITableView tableView shouldHighlightRowAtIndexPath example in Objective C (iOS).


UITableView tableView shouldHighlightRowAtIndexPath

Asks the delegate if the specified row should be highlighted.

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView shouldHighlightRowAtIndexPath]
tableView
The table-view object that is making this request.
indexPath
The index path of the row being highlighted.

Return Value
YES if the row should be highlighted or NO if it should not.

Discussion of [UITableView tableView shouldHighlightRowAtIndexPath]
As touch events arrive, the table view highlights rows in anticipation of the user selecting them. As it processes those touch events, the table view calls this method to ask your delegate if a given cell should be highlighted. Your delegate can implement this method and use it to prevent the highlighting of a row when another row is already selected or when other relevant criteria occur.

If you do not implement this method, the default return value is YES.

UITableView tableView shouldHighlightRowAtIndexPath example.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
   return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // do something here
}

Example of [UITableView tableView shouldHighlightRowAtIndexPath].
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Add your Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor whiteColor] ForCell:cell];  //highlight colour
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Reset Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color

}

- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
    cell.contentView.backgroundColor = color;
    cell.backgroundColor = color;
}

UITableView tableView shouldHighlightRowAtIndexPath example.
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.height;
}

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable;
}

- (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable ? indexPath : nil;
}

End of UITableView tableView shouldHighlightRowAtIndexPath example article.

UITableView tableView indentationLevelForRowAtIndexPath example in Objective C (iOS).


UITableView tableView indentationLevelForRowAtIndexPath

Asks the delegate to return the level of indentation for a row in a given section.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView indentationLevelForRowAtIndexPath]
tableView
The table-view object requesting this information.
indexPath
An index path locating the row in tableView.

Return Value of [UITableView tableView indentationLevelForRowAtIndexPath]
Returns the depth of the specified row to show its hierarchical position in the section.

UITableView tableView indentationLevelForRowAtIndexPath example.
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

            NSInteger theLevel=0;
            if ( indexPath.row==1) {
                theLevel=5;
            }
            return theLevel;
        }

Example of [UITableView tableView indentationLevelForRowAtIndexPath].
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
  if(tableView.editing == YES){
    return 1; // or higher integer
  } else {
    return 0;
  }
}

UITableView tableView indentationLevelForRowAtIndexPath example.
- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [indexPath row] % 2;
}

End of UITableView tableView indentationLevelForRowAtIndexPath example article.

UITableView tableView heightForHeaderInSection example in Objective C (iOS).


UITableView tableView heightForHeaderInSection

Asks the delegate for the height to use for the header of a particular section.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Parameters of [UITableView tableView heightForHeaderInSection]
tableView
The table-view object requesting this information.
section
An index number identifying a section of tableView .

Return Value
A floating-point value that specifies the height (in points) of the header for section.

Discussion of [UITableView tableView heightForHeaderInSection]
This method allows the delegate to specify section headers with varying heights.

Special Considerations
Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView:viewForHeaderInSection: returned a nil view. In iOS 5.0 and later, you must return the actual height for each section header in this method.

UITableView tableView heightForHeaderInSection example.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1 )
        return 0.000001f;
    else return 44.0f; // put 22 in case of plain one..
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001f; //removing section footers
}

Example of [UITableView tableView heightForHeaderInSection].
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return kFirstSectionHeaderHeight;
    return [self sectionHeaderHeight];
}

UITableView tableView heightForHeaderInSection example.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{   
    return [self tableView:tableView sizeForHeaderLabelInSection:section].height + kSectionTitleTopMargin + kSectionTitleBottomMargin;
}

End of UITableView tableView heightForHeaderInSection example article.

UITableView tableView heightForFooterInSection example in Objective C (iOS).


UITableView tableView heightForFooterInSection

Asks the delegate for the height to use for the footer of a particular section.

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

Parameters
tableView
The table-view object requesting this information.
section
An index number identifying a section of tableView .

Return Value of [UITableView tableView heightForFooterInSection]
A floating-point value that specifies the height (in points) of the footer for section.

Discussion of [UITableView tableView heightForFooterInSection]
This method allows the delegate to specify section footers with varying heights. The table view does not call this method if it was created in a plain style (UITableViewStylePlain).

Special Considerations
Prior to iOS 5.0, table views would automatically resize the heights of footers to 0 for sections where tableView:viewForFooterInSection: returned a nil view. In iOS 5.0 and later, you must return the actual height for each section footer in this method.

UITableView tableView heightForFooterInSection example.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (![section hasRow]) return 0;
}
and

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  if (![section hasRow]) return 0.0f;
}

Example of [UITableView tableView heightForFooterInSection].
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 6;
    return 1.0;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 5.0;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

UITableView tableView heightForFooterInSection example.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    switch (section) {
        case 7:
            return 80;
    }
    return 10;
}

End of UITableView tableView heightForFooterInSection example article.

UITableView tableView editingStyleForRowAtIndexPath example in Objective C (iOS).


UITableView tableView editingStyleForRowAtIndexPath

Asks the delegate for the editing style of a row at a particular location in a table view.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView editingStyleForRowAtIndexPath]
tableView
The table-view object requesting this information.
indexPath
An index path locating a row in tableView.

Return Value
The editing style of the cell for the row identified by indexPath.

Discussion of [UITableView tableView editingStyleForRowAtIndexPath]
This method allows the delegate to customize the editing style of the cell located atindexPath. If the delegate does not implement this method and the UITableViewCell object is editable (that is, it has its editing property set to YES), the cell has the UITableViewCellEditingStyleDelete style set for it.

UITableView tableView editingStyleForRowAtIndexPath example.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

Example of [UITableView tableView editingStyleForRowAtIndexPath].
- (UITableViewCellEditingStyle) tableView:(UITableView *) tableView editingStyleForRowAtIndexPath:(NSIndexPath *) indexPath {
    return ([self.tableView isEditing] && indexPath.row == 0) ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}

UITableView tableView editingStyleForRowAtIndexPath example.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// No editing style if not editing or the index path is nil.
if (!indexPath) return UITableViewCellEditingStyleNone;

// No editing style if the tableView has no cells.
if ([self.array count] == 0) return UITableViewCellEditingStyleNone;

// If tableView is not editing, then return delete button.
if (tableView.editing == NO) return UITableViewCellEditingStyleDelete;

// If tableView is editing, then return delete button too.
if (tableView.editing == YES) return UITableViewCellEditingStyleDelete;

// If none of the above are returned, then return \"none\".
return UITableViewCellEditingStyleNone;
}

End of UITableView tableView editingStyleForRowAtIndexPath example article.

UITableView tableView didDeselectRowAtIndexPath example in Objective C (iOS).


UITableView tableView didDeselectRowAtIndexPath

Tells the delegate that the specified row is now deselected.

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView didDeselectRowAtIndexPath]
tableView
A table-view object informing the delegate about the row deselection.
indexPath
An index path locating the deselected row in tableView.

Discussion of [UITableView tableView didDeselectRowAtIndexPath]
The delegate handles row deselections in this method. It could, for example, remove the check-mark image (UITableViewCellAccessoryCheckmark) associated with the row.

UITableView tableView didDeselectRowAtIndexPath example.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView reloadData];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return indexPath;
}

Example of [UITableView tableView didDeselectRowAtIndexPath].
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyViewController *prof = [[MyViewController alloc]initWithNibName:@\"MyViewController\" bundle:nil];
prof.hidesBottomBarWhenPushed = YES;
[prof loadWebPage:[[[details objectForKey:@\"Place\"]objectAtIndex:indexPath.section]objectForKey:@\"map_url\" ]];

[self.navigationController pushViewController:prof animated:YES];
}

End of UITableView tableView didDeselectRowAtIndexPath example article.

Friday, June 7, 2013

UITableView UITableViewAutomaticDimension example in Objective C (iOS).


UITableView UITableViewAutomaticDimension

Default Dimension
The default value for a given dimension.

UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension;

Constants
UITableViewAutomaticDimension
Requests that UITableView use the default value for a given dimension.

Discussion of [UITableView UITableViewAutomaticDimension]
You return this value from UITableViewDelegate methods that request dimension metrics when you want UITableView to choose a default value. For example, if you return this constant in the tableView:heightForHeaderInSection: or tableView:heightForFooterInSection:, UITableView uses a height that fits the value returned from tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: (if the title is not nil).

UITableView UITableViewAutomaticDimension example.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == CUSTOM_SECTION)
    {
        return CUSTOM_VALUE;
    }
    return UITableViewAutomaticDimension;
}

Example of [UITableView UITableViewAutomaticDimension].
 the code for your case would be:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 3) {
       return 5;
    } else {
       return UITableViewAutomaticDimension;
    }
}

UITableView UITableViewAutomaticDimension example.
if you implement viewForHeaderInSection you must also implement heightForHeaderInSection. Implement it like this to make sure that it gets the right size for any number of lines:

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return UITableViewAutomaticDimension;
}

End of UITableView UITableViewAutomaticDimension example article.

UITableView UITableViewIndexSearch example in Objective C (iOS).


UITableView UITableViewIndexSearch

Requests icon to be shown in the section index of a table view.

UIKIT_EXTERN NSString *const UITableViewIndexSearch;
Constants
UITableViewIndexSearch
If the data source includes this constant string in the array of strings it returns in sectionIndexTitlesForTableView:, the section index displays a magnifying glass icon at the corresponding index location. This location should generally be the first title in the index.
UITableView UITableViewIndexSearch example.
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray* indexTitles = [NSMutableArray arrayWithObject:UITableViewIndexSearch];  // add magnifying glass
    [indexTitles addObjectsFromArray:[self.fetchedResultsController sectionIndexTitles]];
    return indexTitles;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (title == UITableViewIndexSearch) {
     // if magnifying glass
        [self.resultsTable scrollRectToVisible:self.searchDisplayController.searchBar.frame animated:NO];
     return -1;
    }
    else
     return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index-1];
}

Example of [UITableView UITableViewIndexSearch].
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index==0){
        [tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        return -1;
    }
...
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        NSMutableArray * retValue =[NSMutableArray arrayWithArray:[YOUR ARRAY]];
        [retValue insertObject:UITableViewIndexSearch atIndex:0];
       return retValue;
 } 

UITableView UITableViewIndexSearch example.
- (NSInteger)tableView:(UITableView *)tv sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSInteger section = -1;
    if ([title isEqualToString:UITableViewIndexSearch]) {
        CGRect searchBarFrame = self.searchDisplayController.searchBar.frame;
        [tv scrollRectToVisible:searchBarFrame animated:NO];
    }
    else {
        section = whatever logic you use to determine section
    }
    return section;
}

End of UITableView UITableViewIndexSearch example article.

UITableView UITableViewRowAnimationMiddle example in Objective C (iOS).


UITableView UITableViewRowAnimationMiddle

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationMiddle]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationMiddle example.
-(void)tableView:(UITableView *)tableView commitEditingStyle: UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
                [history removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView endUpdates];
        [tableView reloadData];
       }
}

Example of [UITableView UITableViewRowAnimationMiddle].
if (indexpath) {
      [self.tableView beginUpdates];
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationMiddle];
      [self.tableView endUpdates];
}

UITableView UITableViewRowAnimationMiddle example.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray* toReload = [NSArray arrayWithObjects: indexPath, self.selectedIndexPath, nil];
    self.selectedIndexPath = indexPath;

    [tableView reloadRowsAtIndexPaths: toReload withRowAnimation: UITableViewRowAnimationMiddle];
}

End of UITableView UITableViewRowAnimationMiddle example article.

UITableView UITableViewRowAnimationNone example in Objective C (iOS).


UITableView UITableViewRowAnimationNone

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationNone]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationNone example.
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

Example of [UITableView UITableViewRowAnimationNone].
Once you have the indexPath of your cell, you can do something like:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

In Xcode 4.6 and higher:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
You can set whatever your like as animation effect, of course.

UITableView UITableViewRowAnimationNone example.
- (void)reloadRow0Section0 {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [indexPaths release];
}

End of UITableView UITableViewRowAnimationNone example article.

UITableView UITableViewRowAnimationBottom example in Objective C (iOS).


UITableView UITableViewRowAnimationBottom

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationBottom]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationBottom example.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.routineTableView setEditing:editing animated:animated];

if(editing){
    [self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
    currentNumberOfSections++;
} else {
    // delete section
}

Example of [UITableView UITableViewRowAnimationBottom].
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController

    NSMutableArray* paths = [[NSMutableArray alloc] init];

    // fill paths of insertion rows here

    if( editing )
        [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
    else
        [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];

    [paths release];
}

UITableView UITableViewRowAnimationBottom example.
// set the guests arrival status and use animation
[guestList beginUpdates];
if (!guest.didArrive) {
    [guest setDidArrive:YES];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
} else {
    [guest setDidArrive:NO];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}
[guestList endUpdates];
[guestList reloadData];

End of UITableView UITableViewRowAnimationBottom example article.

UITableView UITableViewRowAnimationTop example in Objective C (iOS).


UITableView UITableViewRowAnimationTop

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationTop]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationTop example.
-(void)sliderValueChanged:(id)slider {
   slide = slider.on;

   [tableView beginUpdates];

   if (slider.on) {
      [tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by inserting new section
   } else {
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by removing approprite section
   }

   [tableView endUpdates];
}

Example of [UITableView UITableViewRowAnimationTop].
noOfSections++;
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationTop];

// update your array before calling insertRowsAtIndexPaths: method
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:2]]
                      withRowAnimation:UITableViewRowAnimationTop];

UITableView UITableViewRowAnimationTop example.
        if (_selectedPath != indexPath){
            //delete the row that we selected last time
            if (_selectedPath != nil){
                pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row inSection:_selectedPath.section];
                [_tableView beginUpdates];
                [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
                [_tableView endUpdates];
            }

            //add new row in the section we selected
            _selectedPath = indexPath;
            [_tableView beginUpdates];
            [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
            [_tableView endUpdates];
        }

End of UITableView UITableViewRowAnimationTop example article.

UITableView UITableViewRowAnimationLeft example in Objective C (iOS).


UITableView UITableViewRowAnimationLeft

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationLeft]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationLeft example.
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:rowToMove]
                 withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToMoveTo]
                 withRowAnimation:UITableViewRowAnimationLeft];
// update your dataSource as well.
[tableView endUpdates];

Example of [UITableView UITableViewRowAnimationLeft].
[CATransaction begin];
[CATransaction setCompletionBlock:^{
    [self.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationRight];
}];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationLeft];

[CATransaction commit];

UITableView UITableViewRowAnimationLeft example.
-(void)deleteRecord:(NSInteger)recordNo:(NSInteger)sectionNo:(BOOL)isEditMode:(BOOL)isAnimate {

if(isEditMode){
    NSIndexPath *indexP=[NSIndexPath indexPathForRow:recordNo inSection:sectionNo];
    [tvController.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
else {

    if(isAnimate)
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationFade];
    else
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationNone];

    [tvController.tableView reloadSectionIndexTitles];
    self.navigationItem.hidesBackButton = editing;

}

End of UITableView UITableViewRowAnimationLeft example article.