Apple iphone SDK Using Table Sights
Step 1: Read the actual Hello World tutorial
In case you are are unfamiliar with building in Objective C as well as IPhone SDK please take the
opportunity to go through the sooner tutorial. It also contains instructions for you to install XCode and the
apple iphone SDK.
Step 2: Develop a project
SelectFile > New Task or Apple Key + Shft + N to create up the new task menu. Select the Applications
item from the IPhone OS section through the menu on the remaining, and select View Based Application
through the icons on the correct. When prompted enter the project name, I used TableView in the small
sample code.
There are four files within the Classes package;
TableViewTutorialAppDelegate. l
TableViewTutorialAppDelegate. m
TableViewTutorialViewController. l
TableViewTutorialViewController. m
Step only two: Create the Data Control Class
In addition to the actual four files generated automatically we have to create a new course (and hence
assosiated header as well as source files) to store the information to be displayed within the view. Select File
> Brand new File or press the actual the Command Key + N to open the brand new File Dialog. Create a
brand new source file of kind NSObject Subclass (I utilized the name DataController. m) and be sure you
select the checkbox tagged Also create "DataController. h" file (This will change based on the title you gave
your resource file).
In the DataController. h file add a case of NSMutableArray to store actual data as well as add helper
methods to come back size of array, item in a particular index and to include and delete items. Include a
property corresponding towards the list to allow accessibility. The complete file should look something
similar to this:
1 #import
only two @interface DataController: NSObject
3
4 NSMutableArray *list;
5
a few - (unsigned)countOfList; //returns amount of elements in list
8 - (id)objectInListAtIndex: (unsigned)theIndex; //returns item at given index
7 - (void)addData: (NSString*)data; //adds data towards the list
9 - (void)removeDataAtIndex: (unsigned)theIndex;
twelve @property (nonatomic, copy, readwrite) NSMutableArray *list;
11@end
Step two b: Implement methods associated with Data Controller
First synthesize the getters and setters for your list proporty by including @synthesize to after collection 11
in DataController. mirielle. Now implement the methods defined within the header file.
Method: countOfListThe simplest method may be the list count, just call the count method within the list
property and return the end result.
1 - (unsigned)countOfList
2
3 return [list count];
4
Technique: objectInListAtIndexNext and just as basic is to return the element in a specific index of the
actual list just call the objectInListAtIndex approach to the list and return the end result.
1 - (id)objectInListAtIndex: (unsigned)theIndex
2
3 return [list objectAtIndex:theIndex];
4
Technique: removeDataAtIndex
Just add the phone call to the list framework.
1 - (void)removeDataAtIndex: (unsigned)theIndex
2
3 [list removeObjectAtIndex:theIndex];
4
Technique: addDataI am currently utilizing an NSString to store data however, you probably want to
produce a domain object in a genuine program.
1 - (void)addData: (NSString*)data;
2
3 [list addObject:data];
4
Technique: setListWe also override the actual set list method to ensure the mutable array continues to be
mutable.
1 // Custom set accessor to guarantee the new list is mutable
only two - (void)setList: (NSMutableArray *)newList
3
4 if (list != newList)
5
6 [list release];
7 list = [newList mutableCopy];
8
9
Technique: Init and deallocUsed in order to initilize the objects as well as free mermory respectively.
a single - (id)init
2
3 if (self = [super init])
4
5 //Instantiate list
6 NSMutableArray *localList = [[NSMutableArray alloc] init];
7 self.list = localList;
8 [localList release];
9
10 //Add initial Data
11 [self addData:@"Item 1"];
12 [self addData:@"Item 2"];
13
14 return self;
15
a single - (void)dealloc
2
3 [list release];
4 [super dealloc];
5
Step three: Update Table View Control
Currently the TableViewTutorialViewController inherits through the UIViewController class we have to
change this to the actual UITableViewController. Moreover we have to add a data controller class to provide
data to be displayed within the rows of the desk view. For this we are going to add the @class declarative to
the view controller header file as well as create an instance of the data view class and put in a property for
that example. In addition we also develop a view to store the tableview and develop a corresponding
property. The program code for TableViewTutorialViewController. h should look something similar to this:
1 #import
only two @class DataController;
3 @interface TableViewTutorialViewController: UITableViewController
4
5 DataController *dataController;
6 UIView * myView;
7
7
9 @property (nonatomic, retain) DataController *dataController;
twelve @property (nonatomic, retain) UIView 5. myView;
11@end
Step three: Implement Table View Controller ClassThe code for your of TableViewTutorialViewController.
m file is shown below I am going to individually explain the objective of individual lines or categories of
lines. Broadly what we have been doing is drawing the actual interface (See initWithStyle as well as
loadView), connecting it for this controller object (See loadView collection 24 and 25) after which
implementing the callbacks as well as event handlers. (See other mehods)
//Line 1 should already exist within the auto generated file however add line to because we will be using
methods
//from the information controller class.
1 #import "TableViewTutorialViewController. h"
only two #import "DataController. h"
//Line 3 is car generated but we include lines 4 nd 5 to produce getters and setters for your corresponding
attributes
3 @implementation TableViewTutorialViewController
three @synthesize dataController;
5 @synthesize myView;
Technique: initWithStyle //Constructor Equivelent: utilized to initilize view controller (self) as well as data
controler
6 instruction (id)initWithStyle: (UITableViewStyle)style
7
8 if (self = [super initWithStyle:style])
9
10 self.dataController = [[DataController alloc] init];
11
12
13 return self;
14
Technique: loadView
//Define the interface and connect with controller object by indicating self as delegate and databases
15-(void)loadView
16
17 // create and configure the view
18 CGRect cgRct = CGRectMake(0, 10, 320, 400); //define size and position of view
19 myView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
20 myView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
21 self.view = myView; //set view property of controller to the newly created view
22 UITableView * tableView = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
23 tableView.editing = YES; //This allows user of progrm to add or remove elements from list
24 tableView.dataSource = self;
25 tableView.delegate = self; //make the current object the event handler for view
26
27 [self.view addSubview:tableView];
28
Method: numberOfSectionsInTableView
//We need to implement this as the thing is the data source for your table view
//Hard coded amount of sections in table to at least one as we are only creating a single list for this particular
example
29- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
30
31 return 1;
32
Technique: numberOfRowsInSection//We have only one section which section has a single datasource and
we just return the amount of elements in
//the datasource. We have the plus one because we would like to add a speacial item towards the top of the
list which allows //us to include more items to the actual list. We see how which is done later
33- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section
34
35 // Only one section so return the number of items in the list
36 return [dataController countOfList]+1;
37
Technique: cellForRowAtIndexPath
//This is a call back invoked through the interface when drawing the actual table view. This method will
develop a cell for each
// row and include text to each cell dependeing within the string retrieved from the actual datasource. Note
this is necesary each
/index from zero towards the number or rows returned through the previous method
(numberOfRowsInSection). The actual zeroth
//row is hard coded to show the text "New Item" this really is used to add new rows towards the table.
38- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath
*)indexPath
39
//Try to get rusable cell
40 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
41 if (cell == nil)
42
//If not possible create a new cell
43 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0)
reuseIdentifier:@"CellIdentifier"]
autorelease];
44 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
45
46 // Get the string to display and set the value in the cell
47 if(indexPath.row == 0)
48
//The first (or zeroth cell) contains a New Item string and is used to add elements to list
49 cell.text = @"New Item...";
50
51 else
52
//Retreive text from datasource, the -1 accounts for the first element being hardcoded to say new Item
53 cell.text = [dataController objectInListAtIndex:indexPath.row-1];
54
55 return cell;
56
Technique: editingStyleForRowAtIndexPath
//This defines for every row its editing design, i. e. whether this shows a remove indication (Red circle with
take away sign) or
//and include sign (Green circle along with addition sign). I have hard coded the very first row (the one
which says "New Item") to show the add sign and others to display the take away sign.
57- (UITableViewCellEditingStyle)tableView: (UITableView *)tableView
editingStyleForRowAtIndexPath:
(NSIndexPath *)indexPath
58
59 if(indexPath.row == 0)
60
61 return UITableViewCellEditingStyleInsert;
62
63 else
64
65 return UITableViewCellEditingStyleDelete;
66
67
Technique: commitEditingStyle //This method is invoked once the user has finished editing among the rows
of the desk. The three parameters
//respectivly proivide, the actual table being edited, the type of the row being modified (Add or Delete) and
also the row being
//edited. When the style is delete we take away the corresponding item from the information source and then
remove the row from
///the see. If the style had been add we add another element towards the data source and relode the
information into the table see.
//In reality add item will most likely load a new view that allows the user to enter text but which is left to
another
//tutorial for the time being we are hard coding the written text to be added.
68- (void)tableView: (UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath: (NSIndexPath *)indexPath
69
70 // If row is deleted, remove it from the list.
71 if (editingStyle == UITableViewCellEditingStyleDelete)
72
73 [dataController removeDataAtIndex:indexPath.row-1];
74 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
75
76 else if(editingStyle == UITableViewCellEditingStyleInsert)
77
78 [dataController addData:@"New Row Added"];
79 [tableView reloadData];
80
81
82 instruction (void)dealloc
83
84 [super dealloc];
85
86 @end
Stage 5: Loading the TableViewIn the actual TableViewTutorialAppDelegate. m file's
applicationDidFinishLaunching technique initialize the tableview control and add it like a sub view. The
code should look something similar to this.
1 - (void)applicationDidFinishLaunching: (UIApplication *)application
2
3 viewController = [[TableViewTutorialViewController alloc] initWithStyle:UITableViewStylePlain];
5 [window addSubview:viewController.view];
6 [window makeKeyAndVisible];
7
Stage 6: Try it away
This the interface you need to see when you operate your code. Click on the Red remove sign and you may
see a delete switch
Clicking on the delete button should take away the corresponding row from the actual table, see below.
Now click on the green add button along with a new row should appear as stated earlier currently the text
when the new button has already been hard coded.
I hope this guide was clear and helpful allow me to know if there tend to be any problems or if it may be
improved. Also please down load the XCode project through my site
more about iphone 4s