Saturday, June 8, 2013

UINavigationController viewControllers example in Objective C (iOS).


UINavigationController viewControllers

The view controllers currently on the navigation stack.

@property(nonatomic, copy) NSArray *viewControllers

Discussion of [UINavigationController viewControllers]
The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to NO.

UINavigationController viewControllers example.
UINavigationControllers have a property called viewControllers as you have stated above. Since this is an array of View Controllers, referencing a specific view controller in this hierarchy is no different than accessing any other object in an array.

UIViewController *theControllerYouWant = [self.navigationController.viewControllers objectAtIndex:(theIndexOfYourViewController)];
In addition check out the View Controller's Programming Guide for iOS specifically the section called 'Modifying the Navigation Stack'.

Example of [UINavigationController viewControllers].
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[array removeObjectAtIndex:1];

self.navigationController.viewControllers = array;
Or just use

[self.navigationController setViewControllers:array animated:NO];

UINavigationController viewControllers example.
Pretty Simple, when about to push the thirdViewController inseat of doing a simple pushViewController do this:

NSArray * viewControllers = [self.navigationController viewControllers];
NSArray * newViewControllers = [NSArray arrayWithObjects:[viewControllers objectAtIndex:0], [viewControllers objectAtIndex:1], thirdController,nil];
[self.navigationController setViewControllers:newViewControllers];
where [viewControllers objectAtIndex:0] and [viewControllers objectAtIndex:1] are your rootViewController and your FirstViewController.

End of UINavigationController viewControllers example article.

UINavigationController setToolbarHidden animated example in Objective C (iOS).


UINavigationController setToolbarHidden animated

Changes the visibility of the navigation controller’s built-in toolbar.

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden
Specify YES to hide the toolbar or NO to show it.
animated
Specify YES if you want the toolbar to be animated on or off the screen.

Discussion of [UINavigationController setToolbarHidden animated]
You can use this method to animate changes to the visibility of the built-in toolbar.

Calling this method with the animated parameter set to NO is equivalent to setting the value of the toolbarHidden property directly. The toolbar simply appears or disappears depending on the value in the hidden parameter.

UINavigationController setToolbarHidden animated example.
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:NO animated:animated];
    [super viewWillAppear:animated];
}
To hide toolbar:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

Example of [UINavigationController setToolbarHidden animated].
//set up the toolbar
[self.navigationController setToolbarHidden:NO animated:NO];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];  //for example

//set the toolbar buttons
 [self setToolbarItems:[NSArray arrayWithObjects:button1, button2, nil]];  

UINavigationController setToolbarHidden animated example.
- (void)viewDidAppear:(BOOL)animated {
    [[self tableView] reloadData];

    [[self navigationController] setToolbarHidden: NO animated:YES];   
    UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
    [self setToolbarItems:arr animated:YES];

    [super viewDidAppear:animated];
}

End of UINavigationController setToolbarHidden animated example article.

UINavigationController setToolbarHidden example in Objective C (iOS).


UINavigationController setToolbarHidden

Changes the visibility of the navigation controller’s built-in toolbar.

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden
Specify YES to hide the toolbar or NO to show it.
animated
Specify YES if you want the toolbar to be animated on or off the screen.

Discussion of [UINavigationController setToolbarHidden]
You can use this method to animate changes to the visibility of the built-in toolbar.

Calling this method with the animated parameter set to NO is equivalent to setting the value of the toolbarHidden property directly. The toolbar simply appears or disappears depending on the value in the hidden parameter.

UINavigationController setToolbarHidden example.
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:NO animated:animated];
    [super viewWillAppear:animated];
}
To hide toolbar:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

Example of [UINavigationController setToolbarHidden].
//set up the toolbar
[self.navigationController setToolbarHidden:NO animated:NO];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];  //for example

//set the toolbar buttons
 [self setToolbarItems:[NSArray arrayWithObjects:button1, button2, nil]];  

UINavigationController setToolbarHidden example.
- (void)viewDidAppear:(BOOL)animated {
    [[self tableView] reloadData];

    [[self navigationController] setToolbarHidden: NO animated:YES];   
    UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
    [self setToolbarItems:arr animated:YES];

    [super viewDidAppear:animated];
}

End of UINavigationController setToolbarHidden example article.

UINavigationController setNavigationBarHidden animated example in Objective C (iOS).


UINavigationController setNavigationBarHidden animated

Sets whether the navigation bar is hidden.

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden
Specify YES to hide the navigation bar or NO to show it.
animated
Specify YES if you want to animate the change in visibility or NO if you want the navigation bar to appear immediately.

Discussion of [UINavigationController setNavigationBarHidden animated]
For animated transitions, the duration of the animation is specified by the value in the UINavigationControllerHideShowBarDuration constant.

UINavigationController setNavigationBarHidden animated example.
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

Example of [UINavigationController setNavigationBarHidden animated].
- (void) viewWillDisappear:(BOOL)animated
{
    if (self.navigationController.topViewController != self)
    {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }

    [super viewWillDisappear:animated];
}

UINavigationController setNavigationBarHidden animated example.
For the UINavigationController:

[UINavigationBar beginAnimations:@"NavBarFade" context:nil];
self.navigationController.navigationBar.alpha = 1;
[self.navigationController setNavigationBarHidden:YES animated:NO]; //Animated must be NO!
[UINavigationBar setAnimationCurve:UIViewAnimationCurveEaseIn];
[UINavigationBar setAnimationDuration:1.5];
self.navigationController.navigationBar.alpha = 0;
[UINavigationBar commitAnimations];
For the UITabBarController:

[UITabBar beginAnimations:@"TabBarFade" context:nil];
self.tabBarController.tabBar.alpha = 1;
[self.tabBarController.tabBar setHidden:YES];
[UITabBar setAnimationCurve:UIViewAnimationCurveEaseIn];
[UITabBar setAnimationDuration:1.5];
self.tabBarController.navigationBar.alpha = 0;
[UITabBar commitAnimations];

End of UINavigationController setNavigationBarHidden animated example article.

UINavigationController setNavigationBarHidden example in Objective C (iOS).


UINavigationController setNavigationBarHidden

Sets whether the navigation bar is hidden.

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden
Specify YES to hide the navigation bar or NO to show it.
animated
Specify YES if you want to animate the change in visibility or NO if you want the navigation bar to appear immediately.

Discussion of [UINavigationController setNavigationBarHidden]
For animated transitions, the duration of the animation is specified by the value in the UINavigationControllerHideShowBarDuration constant.

UINavigationController setNavigationBarHidden example.
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

Example of [UINavigationController setNavigationBarHidden].
- (void) viewWillDisappear:(BOOL)animated
{
    if (self.navigationController.topViewController != self)
    {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }

    [super viewWillDisappear:animated];
}

UINavigationController setNavigationBarHidden example.
For the UINavigationController:

[UINavigationBar beginAnimations:@"NavBarFade" context:nil];
self.navigationController.navigationBar.alpha = 1;
[self.navigationController setNavigationBarHidden:YES animated:NO]; //Animated must be NO!
[UINavigationBar setAnimationCurve:UIViewAnimationCurveEaseIn];
[UINavigationBar setAnimationDuration:1.5];
self.navigationController.navigationBar.alpha = 0;
[UINavigationBar commitAnimations];
For the UITabBarController:

[UITabBar beginAnimations:@"TabBarFade" context:nil];
self.tabBarController.tabBar.alpha = 1;
[self.tabBarController.tabBar setHidden:YES];
[UITabBar setAnimationCurve:UIViewAnimationCurveEaseIn];
[UITabBar setAnimationDuration:1.5];
self.tabBarController.navigationBar.alpha = 0;
[UITabBar commitAnimations];

End of UINavigationController setNavigationBarHidden example article.

UINavigationController topViewController example in Objective C (iOS).


UINavigationController topViewController

The view controller at the top of the navigation stack. (read-only)

@property(nonatomic, readonly, retain) UIViewController *topViewController

UINavigationController topViewController example.
- (void)applicationDidFinishLaunching:(UIApplication *)application {

    RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
    rootViewController.managedObjectContext = self.managedObjectContext;

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

Example of [UINavigationController topViewController].
Try topViewController instead of visibleViewController.

FirstViewController *fVC = [navController topViewController];

UINavigationController topViewController example.
Implement the UINavigationControllerDelegate method:

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
and query it to find out the currently displayed view controller:

 navigationController.topViewController
This is the one you're coming from.


End of UINavigationController topViewController example article.

UINavigationController toolbarHidden example in Objective C (iOS).


UINavigationController toolbarHidden

A Boolean indicating whether the navigation controller’s built-in toolbar is visible.

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden

Discussion of [UINavigationController toolbarHidden]
If this property is set to YES, the toolbar is not visible. The default value of this property is YES.

UINavigationController toolbarHidden example.
To show toolbar in new view controller just add this:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:NO animated:animated];
    [super viewWillAppear:animated];
}
To hide toolbar:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

Example of [UINavigationController toolbarHidden].
-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
  self = [super initWithNibName:name bundle:bundle];
  if (self) {
    self.title = @"My Title";
    NSArray* toolbarItems = [NSArray arrayWithObjects:
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                      target:self
                                                      action:@selector(addStuff:)],
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                                      target:self
                                                      action:@selector(searchStuff:)],
        nil];
    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;
    self.navigationController.toolbarHidden = NO;
  }
  return self;
}

UINavigationController toolbarHidden example.
- (void)viewDidLoad { 
    [super viewDidLoad]; 
 
    self.title = @"My View Controller"; 
    // hide the navigation bar 
    // use setToolbarHidden:animated: if you need animation 
    self.navigationController.toolbarHidden = NO; 
}  

End of UINavigationController toolbarHidden example article.

UINavigationController navigationBarHidden example in Objective C (iOS).


UINavigationController navigationBarHidden

A Boolean value that determines whether the navigation bar is hidden.

@property(nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden

Discussion of [UINavigationController navigationBarHidden]
If YES, the navigation bar is hidden. The default value is NO. Setting this property does not animate the hiding or showing of the navigation bar; use setNavigationBarHidden:animated: for that purpose.

UINavigationController navigationBarHidden example.
- (void)viewDidLoad
{
    [super viewDidLoad];
     self.title = @"My Title";
     self.navigationController.navigationBarHidden = YES; //YES : 숨기기, NO : 보이기
}

Example of [UINavigationController navigationBarHidden].
CATransition *transition = [CATransition animation];
transition.duration = kAnimationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:tableViewController animated:YES];

UINavigationController navigationBarHidden example.
Use this in first view controller:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:YES animated:animated];
}

End of UINavigationController navigationBarHidden example article.

UINavigationController navigationBar example in Objective C (iOS).


UINavigationController navigationBar

The navigation bar managed by the navigation controller. (read-only)

@property(nonatomic, readonly) UINavigationBar *navigationBar

Discussion of [UINavigationController navigationBar]
It is permissible to modify the barStyle or translucent properties of the navigation bar but you must never change its frame, bounds, or alpha values directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.

UINavigationController navigationBar example.
If you really want the navigation bar and you're targeting 3.2 and up I would recommend to attach an UITapGestureRecognizer to the navigationBar.

- (void)viewDidLoad {
    UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
              initWithTarget:self action:@selector(navigationBarDoubleTap:)];
    tapRecon.numberOfTapsRequired = 2;
    [navController.navigationBar addGestureRecognizer:tapRecon];
    [tapRecon release];
}

- (void)navigationBarDoubleTap:(UIGestureRecognizer*)recognizer {
    [tableView setContentOffset:CGPointMake(0,0) animated:YES];
}

Example of [UINavigationController navigationBar].
// Initialise the navigation controller with the first view controller as its root view controller
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

// Add your code here
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
// I have commented out the set translucent attribute, as it has some interesting effects.  Try uncommenting it :).
// [navigationController.navigationBar setTranslucent:YES];
[navigationController.navigationBar setTintColor:[UIColor redColor]];

// If you wish to hide the navigation controller
// Note: We are able to alternatively hide or show the navigation bar by using
// [self.navigationController setNavigationBarHidden:YES] in our viewWillAppear method
// within the specific UIViewController that we wish to hide/show the navigation bar
[navigationController setNavigationBarHidden:YES];

// Navigation controller has copy of view controller, so release our copy
[rootViewController release];

UINavigationController navigationBar example.
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"My Title";
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255.0/255
                                                                                  green:10.0/255 blue:100.0/255 alpha:1];
}

- (void)viewDidLoad
{
   super viewDidLoad];
   self.title = @"My Title";
   self.navigationController.navigationBar.barStyle = UIBarStyleBlack; //스타일 적용
   self.navigationController.navigationBar.translucent = YES; // 반투명 효과 주기
}

End of UINavigationController navigationBar example article.

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


UITableView tableView willDeselectRowAtIndexPath

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

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

Parameters
tableView
A table-view object informing the delegate about the impending deselection.
indexPath
An index path locating the row in tableView to be deselected.

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

Discussion of [UITableView tableView willDeselectRowAtIndexPath]
This method is only called if there is an existing selection when the user tries to select a different row. The delegate is sent this method for the previously selected row. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down.

UITableView tableView willDeselectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)aTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
    if (![selectedIndexes containsObject:friend]) {
        [selectedIndexes addObject:friend];

    }
    else {
        [selectedIndexes removeObject:friend];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        return nil;
    }
    return indexPath;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
    if (![selectedIndexes containsObject:friend]) {
         return indexPath;
     }
    // do not select so we can have multiple select
    return nil;
}

Example of [UITableView tableView willDeselectRowAtIndexPath].
-(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;
}

UITableView tableView willDeselectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];

  return indexPath;

}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

return indexPath;

}

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


UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath

Changes the default title of the delete-confirmation button.

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

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

Return Value
A localized string to used as the title of the delete-confirmation button.

Discussion of [UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath]
By default, the delete-confirmation button, which appears on the right side of the cell, has the title of “Delete”. The table view displays this button when the user attempts to delete a row, either by swiping the row or tapping the red minus icon in editing mode. You can implement this method to return an alternative title, which should be localized.

UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath example.
@interface CategoryAddViewController : UITableViewController <UITableViewDelegate>
@end

@implementation CategoryAddViewController
// ...
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"Please don't delete me!";
}

@end

Example of [UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath].

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Close";
}

UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath example.
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Cancel";
}

End of UITableView tableView titleForDeleteConfirmationButtonForRowAtIndexPath 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.