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

Prob­lem:

your class has to sup­port the  UITable­View del­e­gate and/or data­source and is a sim­ple UITable­View­Con­troller os NSob­ject subclass.

Descrip­tion:

While devel­op­ing on the iPhone, using XCode,  I found really annoy­ing to recre­ate all the uitable­view del­e­gate and data­source meth­ods if the class was before an NSOb­ject or a UIV­iew­Con­troller with­out any table, for now the only way was to do a copy and paste from another class or to cre­ate a new one class that is a sub­class of UITableViewController.

The Solu­tion:

  1. XCode: goto Scripts menu->Edit User Scripts
  2. Cre­ate a new Script
  3. Insert the fol­low­ing Code:
    #! /usr/bin/perl –w
    1.  
    2. # Insert UITable­View default meth­ods (del­e­gate and data­source) into a class.
    3. # insertTableViewMethod.pl — inserts del­e­gate and data­source meth­ods 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. // Cus­tomize the num­ber of rows in the table view.
    14. - (NSInteger)tableView:(UITableView *)table­View numberOfRowsInSection:(NSInteger)section {
    15. return 0;
    16. }
    17.  
    18. // Cus­tomize the appear­ance of table view cells.
    19. - (UITable­View­Cell *)tableView:(UITableView *)table­View cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    20. sta­tic NSString *Cel­lI­den­ti­fier = @\“Cell\”;
    21. UITable­View­Cell *cell = [table­View dequeueReusableCellWithIdentifier:CellIdentifier];
    22. if (cell == nil) {
    23. cell = [[[UITable­View­Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    24. }
    25. // Set up the cell…
    26. return cell;
    27. }
    28.  
    29. - (void)tableView:(UITableView *)table­View didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    30. // Nav­i­ga­tion logic may go here. Cre­ate and push another view controller.
    31. // Anoth­erView­Con­troller *anoth­erView­Con­troller = [[Anoth­erView­Con­troller alloc] initWithNibName:@\“Anoth­erView\” bundle:nil];
    32. // [self.navigationController pushViewController:anotherViewController];
    33. // [anoth­erView­Con­troller release];
    34. }
    35.  
    36. /*
    37. // Over­ride to sup­port con­di­tional edit­ing of the table view.
    38. - (BOOL)tableView:(UITableView *)table­View canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    39. // Return NO if you do not want the spec­i­fied item to be editable.
    40. return YES;
    41. }
    42. */
    43.  
    44. /*
    45. // Over­ride to sup­port edit­ing the table view.
    46. - (void)tableView:(UITableView *)table­View commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    47. if (edit­ingStyle == UITableViewCellEditingStyleDelete) {
    48. // Delete the row from the data source
    49. [table­View deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    50. }
    51. else if (edit­ingStyle == UITableViewCellEditingStyleInsert) {
    52. // Cre­ate a new instance of the appro­pri­ate class, insert it into the array, and add a new row to the table view
    53. }
    54. }
    55. */
    56.  
    57. /*
    58. // Over­ride to sup­port rear­rang­ing the table view.
    59. - (void)tableView:(UITableView *)table­View moveRowAtIndexPath:(NSIndexPath *)fro­mIn­d­ex­Path toIndexPath:(NSIndexPath *)toIndexPath {
    60. }
    61. */
    62.  
    63. /*
    64. // Over­ride to sup­port con­di­tional rear­rang­ing of the table view.
    65. - (BOOL)tableView:(UITableView *)table­View 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!

Leave a comment

Your comment