Enable any class to be an UITableView delegate+datasource” script.

Problem:

your class has to support the  UITableView delegate and/or datasource and is a simple UITableViewController os NSobject subclass.

Description:

While developing on the iPhone, using XCode,  I found really annoying to recreate all the uitableview delegate and datasource methods if the class was before an NSObject or a UIViewController without any table, for now the only way was to do a copy and paste from another class or to create a new one class that is a subclass of UITableViewController.

The Solution:

  1. XCode: goto Scripts menu->Edit User Scripts
  2. Create a new Script
  3. Insert the following Code:
    #! /usr/bin/perl -w
    1.  
    2. # Insert UITableView default methods (delegate and datasource) into a class.
    3. # insertTableViewMethod.pl – inserts delegate and datasource methods into a class.
    4.  
    5. use strict;
    6. print "
    7. #pragma mark Table view methods
    8.  
    9. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    10. return 1;
    11. }
    12.  
    13. // Customize the number of rows in the table view.
    14. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    15. return 0;
    16. }
    17.  
    18. // Customize the appearance of table view cells.
    19. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    20. static NSString *CellIdentifier = @\"Cell\";
    21. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    22. if (cell == nil) {
    23. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    24. }
    25. // Set up the cell…
    26. return cell;
    27. }
    28.  
    29. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    30. // Navigation logic may go here. Create and push another view controller.
    31. // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@\"AnotherView\" bundle:nil];
    32. // [self.navigationController pushViewController:anotherViewController];
    33. // [anotherViewController release];
    34. }
    35.  
    36. /*
    37. // Override to support conditional editing of the table view.
    38. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    39. // Return NO if you do not want the specified item to be editable.
    40. return YES;
    41. }
    42. */
    43.  
    44. /*
    45. // Override to support editing the table view.
    46. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    47. if (editingStyle == UITableViewCellEditingStyleDelete) {
    48. // Delete the row from the data source
    49. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    50. }
    51. else if (editingStyle == UITableViewCellEditingStyleInsert) {
    52. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    53. }
    54. }
    55. */
    56.  
    57. /*
    58. // Override to support rearranging the table view.
    59. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    60. }
    61. */
    62.  
    63. /*
    64. // Override to support conditional rearranging of the table view.
    65. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    66. // Return NO if you do not want the item to be re-orderable.
    67. return YES;
    68. }
    69. */
    70. ";
    71.  
    72. exit 0;
  4. Have -quick-fun!

Comments (1)

OzOctober 7th, 2011 at 8:34 pm

thank you

Leave a comment

Your comment