Customize UITableViewCell

walden systems, objective c, geolocation, ios, CLLocationManager, desiredAccuracy, distanceFilter, startUpdatingLocation, stopUpdatingLocation, delegate, iphone, picture, screen, swipe, iphone, thread, nsthread, uitableviewcell, uitableview, initwithFName, infocellcontroller
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

In this example, we will create a custom UITableViewCell to hold first name, last name and age. The following will have to be done:
1 personalInfo object to hold the data
2 infoCell app delegate to handle application states
3 infoCellController to manage user interaction

In the object interface file, declare the variables and method :

1     @property (nonatomic, strong) NSString *_first_name;
2     @property (nonatomic, strong) NSString *_last_name;
3     @property (nonatomic, strong) NSInteger *_age;
4 
5     -(id)initWithFname:(NSString *)first_name
6              last_name:(NSString *)last_name
7                    age:(NSInteger)age;

In the object implementation file, create the getters and and setters for the variables. We also need to create the method for initWithFname.

 1    @synthesize _first_name;
 2    @synthesize _last_name;
 3    @synthesize _age;
 4    
 5    -(id)initWithFname:(NSString *)first_name
 6          last_name:(NSString *)last_name
 7                age:(NSInteger)age
 8    {
 8        self = [super init];
10        if (self) 
11        {
12            self._first_name = first_name;
13            self._last_name = last_name;
14            self._age = age;
15        }
16        return self;
17    }


In the app delegate interface file, forward declare the viewcontroller ( in this case infoCellController ) and declare variables.

In the app delegate implementation file, We also need to create a method that will initialize the screen, initialize the new controller, make the new view controoler your roote view and set the background color to make your window visible. We do this by accessing the _curr_window properties.

App delegate interface file

1   @class infoCellController;
2 
3   ...
4   @property (strong, nonatomic) UIWindow *_curr_window;
5   @property (strong, nonatomic) infoCellController *newInfoCellViewController;



App delegate implementation file

1    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2    {
3        self._curr_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
4        self._newInfoCellViewController = [[infoCellController alloc] initWithNibName:@"CustomViewController" bundle:nil];
5        self._curr_window.rootViewController = self._newInfoCellViewController;
6        self._curr_window.backgroundColor = [UIColor whiteColor];
7        [self._curr_window makeKeyAndVisible];
8        return YES;
9    }

in the infoCellController, declare the variables and populate the _personalInfoList with some data

 1    @property (nonatomic, strong) UITableView *_myTableView;
 2    @property (nonatomic, strong) NSMutableArray *_personalInfoList;
 3
 4    NSMutableArray *new_list = [[NSMutableArray alloc] init];
 5    self._personalInfoList = new_list;  
 6    personalInfo *new_info = [[personalInfo alloc] initWithFname:@"John"
 7                                                        last_name:@"Doe"
 8                                                              age:21];
 9    [_personalInfoList addObject:new_info];
10    ...

Create a method to as the data source for a cell to insert in a table view. This action is done by dequeuing an existing cell if one is available or by creating a new cell. We will also create some custom labels and buttons inside the cell view and populate the cell view. We will also add an event listener to the button which will trigger another method.

 1   - (UITableViewCell *) tableView:(UITableView *)tableView
 2           cellForRowAtIndexPath:(NSIndexPath *)indexPath
 3   {
 4     
 5     UITableViewCell *myCellView = nil;
 6     UILabel *firstNameLabel, *lastNameLabel, *ageLabel;
 7     UIButton *detailInfoButton;
 8 
 9     if ([tableView isEqual:self.myTableView])
10     {
11         
12         static NSString *TableViewCellIdentifier = @"personalInfoCell";
13         
14         myCellView = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
15         if (myCellView == nil)
16         {
17             myCellView = [[UITableViewCell alloc]
18                           initWithStyle:UITableViewCellStyleDefault
19                           reuseIdentifier:TableViewCellIdentifier];
20             
21             CGRect myFrame = CGRectMake(10.0, 0.0, 220, 25.0);
22             firstNameLabel = [[UILabel alloc] initWithFrame:myFrame];
23             firstNameLabel.tag = FIRST_NAME_TAG;
24             firstNameLabel.font = [UIFont boldSystemFontOfSize:17.0];
25             firstNameLabel.backgroundColor = [UIColor clearColor];
26             [myCellView.contentView addSubview:firstNameLabel];
27             
28             detailInfoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
29             detailInfoButton.frame = CGRectMake(200.0, 5.0, 90, 25.0);
30             [detailInfoButton setTitle:@"Detail Info"
31                            forState:UIControlStateNormal];
32             [detailInfoButton addTarget:self
33                               action:@selector(detailInfo:)
34                     forControlEvents:UIControlEventTouchUpInside];
35             [myCellView.contentView addSubview:detailInfoButton];
36 
37             myFrame.origin.y += 25;
38             lastNameLabel = [[UILabel alloc] initWithFrame:myFrame];
39             lastNameLabel.tag = LAST_NAME_TAG;
40             lastNameLabel.font = [UIFont systemFontOfSize:17.0];
41             lastNameLabel.backgroundColor = [UIColor clearColor];
42             [myCellView.contentView addSubview:lastNameLabel];
43             
44             myFrame.origin.y += 25;
45             ageLabel = [[UILabel alloc] initWithFrame:myFrame];
46             ageLabel.tag = AGE_TAG;
47             ageLabel.font = [UIFont systemFontOfSize:17.0];
48             ageLabel.backgroundColor = [UIColor clearColor];
49             [myCellView.contentView addSubview:ageLabel];          
50         }
51         else 
52         {
53             firstNameLabel = (UILabel *)[myCellView.contentView viewWithTag:FIRST_NAME_TAG];
54             lastNameLabel = (UILabel *)[myCellView.contentView viewWithTag:LAST_NAME_TAG];
55             ageLabel = (UILabel *)[myCellView.contentView viewWithTag:AGE_TAG];
56         }       
57         personalInfo *new_personalInfo = [[personalInfo alloc] init];
58         new_personalInfo = [self._personalInfoList objectAtIndex:indexPath.row];       
59         firstNameLabel.text = [NSString stringWithFormat:@"Name: %@", new_personalInfo._first_name];
60         lastNameLabel.text = [NSString stringWithFormat:@"Code: %@", new_personalInfo._last_name];
61         ageLabel.text = [NSString stringWithFormat:@"Continent: %@", new_personalInfo._age];       
62     }
63     return myCellView;
64 }