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

Saturday, June 8, 2013

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 viewForHeaderInSection example in Objective C (iOS).


UITableView tableView viewForHeaderInSection

Asks the delegate for a view object to display in the header of the specified section of the table view.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

Parameters of [UITableView tableView viewForHeaderInSection]
tableView
The table-view object asking for the view object.
section
An index number identifying a section of tableView .

Return Value
A view object to be displayed in the header of section .

Discussion of [UITableView tableView viewForHeaderInSection]
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.

UITableView tableView viewForHeaderInSection example.

-(CGFloat) tableView:(UITableView *)tableView
  heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}

-(UIView *) tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    l.text= @"I am a Section Header";
    return l;
}

Example of [UITableView tableView viewForHeaderInSection].
- (CAGradientLayer *) greyGradient {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.startPoint = CGPointMake(0.5, 0.0);
    gradient.endPoint = CGPointMake(0.5, 1.0);

    UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];

    [gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]];
    return gradient;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width = CGRectGetWidth(tableView.bounds);
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
    UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease];
    container.layer.borderColor = [UIColor grayColor].CGColor;
    container.layer.borderWidth = 1.0f;
    CAGradientLayer *gradient = [self greyGradient];
    gradient.frame = container.bounds;
    [container.layer addSublayer:gradient];

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font= [UIFont boldSystemFontOfSize:19.0f];
    headerLabel.shadowOffset = CGSizeMake(1, 1);
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.shadowColor = [UIColor darkGrayColor];
    NSString *title = [self tableView:tableView titleForHeaderInSection:section];
    headerLabel.text = title;
    return container;
}

UITableView tableView viewForHeaderInSection example.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSLog(@"calling view for header");
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 30.0)];

if(section == 0){
// create the label object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(20.0, 0.0, 100.0, 30.0);

headerLabel.text = @"Totals";

NSString *path = [[NSBundle mainBundle] pathForResource:@"images/minus" ofType:@"gif"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
[customView addSubview:imageView];
[customView addSubview:headerLabel];

}
else {
NSLog(@"test 2");
}
return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0){
return 50;
}
return 30;

}

End of UITableView tableView viewForHeaderInSection example article.

UITableView tableView viewForFooterInSection example in Objective C (iOS).


UITableView tableView viewForFooterInSection

Asks the delegate for a view object to display in the footer of the specified section of the table view.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

Parameters of [UITableView tableView viewForFooterInSection]
tableView
The table-view object asking for the view object.
section
An index number identifying a section of tableView .

Return Value
A view object to be displayed in the footer of section .

Discussion of [UITableView tableView viewForFooterInSection]
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForFooterInSection: is also implemented.

UITableView tableView viewForFooterInSection example.
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{

  ...

  CGRect footerFrame = [tv rectForFooterInSection:1];
  CGRect labelFrame = CGRectMake(20, 20, footerFrame.size.width - 40, footerFrame.size.height - 40);

  UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
  UILabel *footerLabel = [[UILabel alloc] initWithFrame:labelFrame];

  [footer addSubview:footerLabel];
  [footerLabel release];

  ...

  return footer;

}

Example of [UITableView tableView viewForFooterInSection].
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
    NSString* sectionFooter = [self tableView:tableView titleForFooterInSection:section];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yourWidth, yourHeight)]; //create a view- the width should usually be the width of the screen
    UILabel* label = [[UILabel alloc] initWithFrame:someFrame];

    label.backgroundColor = [UIColor blueColor];
    label.textColor = [UIColor whiteColor];
    label.text = sectionFooter;

    [view addSubview:label];

    return view;
}

UITableView tableView viewForFooterInSection example.
// Need to refactor so that the label is Public Sharing and Priviate Sharing and the actions work for each switch
- (UIView *) tableView: (UITableView *) tableView
viewForFooterInSection: (NSInteger) section
{
  if (section == 0 || section == 1) {
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    UIView* footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)] autorelease];
    footerView.autoresizesSubviews = YES;
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    footerView.userInteractionEnabled = YES;

    footerView.hidden = NO;
    footerView.multipleTouchEnabled = NO;
    footerView.opaque = NO;
    footerView.contentMode = UIViewContentModeScaleToFill;

    // Add the label
    UILabel*    footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(150.0, -5.0, 120.0, 45.0)];
    footerLabel.backgroundColor = [UIColor clearColor];
    footerLabel.opaque = NO;
    footerLabel.text = @"Sharing";
    footerLabel.textColor = [UIColor tableHeaderAndFooterColor];
    footerLabel.highlightedTextColor = [UIColor tableHeaderAndFooterColor];
    footerLabel.font = [UIFont boldSystemFontOfSize:17];
    footerLabel.shadowColor = [UIColor whiteColor];
    footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
    [footerView addSubview: footerLabel];

    [footerLabel release]; 

    // Add the switch
    UISwitch* footerSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(215.0, 5, 80.0, 45.0)];
    [footerView addSubview: footerSwitch];

    // Return the footerView
    return footerView;
  }
  else return nil;
}

End of UITableView tableView viewForFooterInSection 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 shouldIndentWhileEditingRowAtIndexPath example in Objective C (iOS).


UITableView tableView shouldIndentWhileEditingRowAtIndexPath

Asks the delegate whether the background of the specified row should be indented while the table view is in editing mode.

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

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

Return Value
YES if the background of the row should be indented, otherwise NO.

Discussion of [UITableView tableView shouldIndentWhileEditingRowAtIndexPath]
If the delegate does not implement this method, the default is YES. This method is unrelated to tableView:indentationLevelForRowAtIndexPath:.

UITableView tableView shouldIndentWhileEditingRowAtIndexPath example.
// UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

Example of [UITableView tableView shouldIndentWhileEditingRowAtIndexPath].
- (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:.3];

  [cell.CellTitle setFrame:cgrectMake(x+20, y, w, h)];  // +x
  [...]
  [cell.CellAddress setFrame:f2];

  [UIView commitAnimations];

  return YES;
}

UITableView tableView shouldIndentWhileEditingRowAtIndexPath example.
// Override to prevent indentation of cells in editing mode (in theory)
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
I also used the code below to stop the insert/delete functionality and enable the rows can be moved (easily switched off).

// Select the editing style of each cell
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Do not allow inserts / deletes
        return UITableViewCellEditingStyleNone;
    }

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
     // Return NO if you do not want the item to be re-orderable.
     return YES;
}

End of UITableView tableView shouldIndentWhileEditingRowAtIndexPath 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 performAction forRowAtIndexPath withSender example in Objective C (iOS).


UITableView tableView performAction forRowAtIndexPath withSender

Tells the delegate to perform a copy or paste operation on the content of a given row.

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

Parameters of [UITableView tableView performAction forRowAtIndexPath withSender]
tableView
The table-view object that is making this request.
action
A selector type identifying the copy: or paste: method of the UIResponderStandardEditActions informal protocol.
indexPath
An index-path object locating the row in its section.
sender
The object that initially sent the copy: or paste: message.

Discussion of [UITableView tableView performAction forRowAtIndexPath withSender]
The table view invokes this method for a given action if the user taps Copy or Paste in the editing menu. The delegate can do whatever is appropriate for the action; for example, for a copy, it can extract the relevant cell content for the row at indexPath and write it to the general pasteboard or an application (private) pasteboard. See UIPasteboard Class Reference for further information.

UITableView tableView performAction forRowAtIndexPath withSender 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:));
}

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

Example of [UITableView tableView performAction forRowAtIndexPath withSender].
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
}
if (action == @selector(cut:)) {
[UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
[dataArray replaceObjectAtIndex:indexPath.row withObject:@""];
[tbl reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
if (action == @selector(paste:)) {
NSString *pasteString = [UIPasteboard generalPasteboard].string;
NSString *tmpString = [NSString stringWithFormat:@"%@ %@", [dataArray objectAtIndex:indexPath.row], pasteString];
[dataArray replaceObjectAtIndex:indexPath.row withObject:tmpString];
[tbl reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

End of UITableView tableView performAction forRowAtIndexPath withSender 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 heightForRowAtIndexPath example in Objective C (iOS).


UITableView tableView heightForRowAtIndexPath

Asks the delegate for the height to use for a row in a specified location.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Parameters
tableView
The table-view object requesting this information.
indexPath
An index path that locates a row in tableView.

Return Value of [UITableView tableView heightForRowAtIndexPath]
A floating-point value that specifies the height (in points) that row should be.

Discussion of [UITableView tableView heightForRowAtIndexPath]
The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.

There are performance implications to using tableView:heightForRowAtIndexPath: instead of the rowHeight property. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).

UITableView tableView heightForRowAtIndexPath example.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // We want the UIFont to be the same as what is in the nib,
    //  but we dont want to call tableView dequeue a bunch because its slow.
    //  If we make the font static and only load it once we can reuse it every
    //  time we get into this method
    static UIFont* dynamicTextFont;
    static CGRect textFrame;
    static CGFloat extraHeight;
    if( !dynamicTextFont ) {
        DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        dynamicTextFont = cell.resizeLabel.font;
        CGRect cellFrame = cell.frame;
        textFrame = cell.resizeLabel.frame;
        extraHeight = cellFrame.size.height-textFrame.size.height; // The space above and below the growing field
    }
    NSString* text = .... // Get this from the some object using indexPath

    CGSize  size = [text sizeWithFont:dynamicTextFont constrainedToSize:CGSizeMake(textFrame.size.width, 200000.f) lineBreakMode:UILineBreakModeWordWrap];

    return size.height+extraHeight;
}

Example of [UITableView tableView heightForRowAtIndexPath].
- (void)viewDidLoad
{
    [super viewDidLoad];

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

    for (int i = 0; i < [self.tableView numberOfRowsInSection:0]; i++) {
        if (arc4random() % 5 == 4) {
            [mutableArray addObject:[LCImageCell class]];
        } else {
            [mutableArray addObject:[LCImageCell class]];
        }
    }

    self.cellTypes = [NSArray arrayWithArray:mutableArray];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.cellTypes objectAtIndex:indexPath.row] isEqual:[LCImageCell class]]) {
        [LCImageCell height];  // class method returns static height for an image cell
    } else {
        [LCTextCell height];   // class method returns static height for a text cell
    };
}

UITableView tableView heightForRowAtIndexPath example.
// Inside tableView:cellForRowAtIndexPath:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
// numberOfTextRows is an integer, declared in the class

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
    // This gets the size of the rectangle needed to draw a multi-line string
    self.numberOfTextRows = round(theSize.height / 18);
    // 18 is the size of the font used in the text label
    // This will give us the number of lines in the multi-line string

    if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows < 2)) {
        return 44;
        // 44 is the default row height; use it for empty or one-line cells (or in other table sections)
    } else {
        return theSize.height + 16;
        // 16 seems to provide a decent space above/below; tweak to taste
    }
}

End of UITableView tableView heightForRowAtIndexPath 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 didSelectRowAtIndexPath example in Objective C (iOS).


UITableView tableView didSelectRowAtIndexPath

Tells the delegate that the specified row is now selected.

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

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

Discussion of [UITableView tableView didSelectRowAtIndexPath]
The delegate handles selections in this method. One of the things it can do is exclusively assign the check-mark image (UITableViewCellAccessoryCheckmark) to one row in a section (radio-list style). This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "€œ“Managing Selections”" in Table View Programming Guide for iOS for further information (and code examples) related to this method.

UITableView tableView didSelectRowAtIndexPath example.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    [myArray addObject:cellText];
}

Example of [UITableView tableView didSelectRowAtIndexPath].
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *detail = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    UIView *headerView = (UIView *)[self tableView:tableView viewForHeaderInSection:indexPath.section];
    UILabel *dateLbl = (UILabel *) [headerView viewWithTag:1];

    UILabel *yrLbl = (UILabel *)[headerView viewWithTag:2];

    detail.titleStrng  = dateLbl.text;
    detail.TimeString  = yrLbl.text;

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

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

    //This toggles the checkmark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //This sets the array
        [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
         //This sets the array
        [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];

    }

}

End of UITableView tableView didSelectRowAtIndexPath example article.

UITableView tableView accessoryButtonTappedForRowWithIndexPath example in Objective C (iOS).


UITableView tableView accessoryButtonTappedForRowWithIndexPath

Tells the delegate that the user tapped the accessory (disclosure) view associated with a given row.

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

Parameters of [UITableView tableView accessoryButtonTappedForRowWithIndexPath]
tableView
The table-view object informing the delegate of this event.
indexPath
An index path locating the row in tableView.

Discussion of [UITableView tableView accessoryButtonTappedForRowWithIndexPath]
The delegate usually responds to the tap on the disclosure button (the accessory view) by displaying a new view related to the selected row. This method is not called when an accessory view is set for the row at indexPath.

UITableView tableView accessoryButtonTappedForRowWithIndexPath example.
Use the table view's cellForRowAtIndexPath: method and pass in the index path as the argument.

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}

Example of [UITableView tableView accessoryButtonTappedForRowWithIndexPath].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    ...

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    ...
    cell.accessoryView = button;

    return cell;
}

- (void)checkButtonTapped:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil){
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

UITableView tableView accessoryButtonTappedForRowWithIndexPath example.
I created a @property in the first View Controller:

@property (nonatomic) NSInteger indexPathRow;
then I modified the code like this:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Disclosure Tapped----> %i" ,indexPath.row);
    self.indexPathRow = indexPath.row;
    [self performSegueWithIdentifier:@"IdentityCard" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"IdentityCard"])
    {
        NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
        [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.indexPathRow]];
    }
}

End of UITableView tableView accessoryButtonTappedForRowWithIndexPath example article.

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


UITableView tableView didEndDisplayingCell forRowAtIndexPath

Tells the delegate that the specified cell was removed from the table.

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

Parameters of [UITableView tableView didEndDisplayingCell forRowAtIndexPath]
tableView
The table-view object that removed the view.
cell
The cell that was removed.
indexPath
The index path of the cell.

Discussion of [UITableView tableView didEndDisplayingCell forRowAtIndexPath]
Use this method to detect when a cell is removed from a table view, as opposed to monitoring the view itself to see when it appears or disappears.

UITableView tableView didEndDisplayingCell forRowAtIndexPath example.
 in iOS 6, use didEndDisplayingCell. Thus, it might look like:

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Movie *movie = self.movies[indexPath.row];

    if ([movie isDownloading])
        [movie cancelDownload];
}

Example of [UITableView tableView didEndDisplayingCell forRowAtIndexPath].
  -(void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath)indexPath{
          [NSNotificationCenter defaultCenter];
          [notificationCenter postNotificationName:@"Table Reloaded" object:nil];
    } 

End of UITableView tableView didEndDisplayingCell forRowAtIndexPath 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 UITableViewRowAnimationAutomatic example in Objective C (iOS).


UITableView UITableViewRowAnimationAutomatic

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 UITableViewRowAnimationAutomatic]
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 UITableViewRowAnimationAutomatic example.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultView beginUpdates];
[resultList removeObjectAtIndex:indexPath.row];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];

Example of [UITableView UITableViewRowAnimationAutomatic].
int sectionNumber = 0; //Note that your section may be different

int nextIndex = [currentItems count]; //starting index of newly added items

[myTableView beginUpdates];

for (NSObject *item in itemsToAdd) {
    //Add the item to the data source
    [currentItems addObject:item];

    //Add the item to the table view
    NSIndexPath *path = [NSIndexPath indexPathForRow:nextIndex++ inSection:sectionNumber];
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
}

[myTableView endUpdates];

UITableView UITableViewRowAnimationAutomatic example.
int indexOfNewSection = 4; // TODO: change to meaningful value
[self.tableview beginUpdates];
[self.tableview insertSections:[NSIndexSet indexSetWithIndex:indexOfNewSection]
              withRowAnimation:UITableViewRowAnimationAutomatic];

// Update data model
[self.sections insertObject:sectionObj atIndex:indexOfNewSection];

[self.tableview endUpdates];

End of UITableView UITableViewRowAnimationAutomatic example article.