Leave a Comment:
1 comment

We will see in this application how to implement trackable-settable UIControls embedded in a UITableView.
Step 1: Create a Window base application using template. Give the appliction name “Cell Touch”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: We need to add the resource file called as “checked.png” and “unchecked.png” into the resource folder. Select resources and add files existing sources and select the checked.png and unchecked.png.
Step 4: Now we’ll add Table view controller class to the project. Choose New file -> Select cocoa touch classes group and then select UITableViewController subclass. Give the name TableViewController.
Step 5: In the AppDelegate.h file we added NavigationController. So make the following changes in the file.
1 2 3 4 5 6 |
@interface Cell_TouchAppDelegate : NSObject { UIWindow *window; UINavigationController *navController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navController; |
Step 6: In the AppDelegate.m file make the following changes:
1 2 3 4 |
- (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:navController.view]; [window makeKeyAndVisible]; } |
Step 7: We added dataArray in the TableViewController.h file, so make the following changes:
1 2 3 4 |
@interface TableViewController : UITableViewController { NSMutableArray *dataArray; } |
Step 8: Now double click your MainWindow.xib file and open it to the Interface Builder. Drag NavigationController from the library and place it the MainWindow. First select navigation controller and bring up Identity Inspector now change the class name into the TableViewController.Double click NavigationController and open it, drag tableview from the library and place into the Navigation Controller Window. Select it and bring up Connection Inspector, you will see there are two available connection DataSource and Delegate. Now connect DataSource to the TableViewController and do it once more time for Delegate. Save it and go back to the Xcode.
Step 9: Create a tabledata.plist for holding the data. What we will display the table.
Step 10: Now make the following changes in the TableViewController.m file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
- (void)viewDidLoad { NSString *path = [[NSBundle mainBundle] pathForResource:@"tabledata" ofType:@"plist"]; self.dataArray = [NSMutableArray arrayWithContentsOfFile:path]; } - (void)viewDidUnload { // release the array self.dataArray = nil; } - (void)dealloc { [dataArray release]; [super dealloc]; } #pragma mark - UIViewController delegate methods - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } #pragma mark - UITableView delegate methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.dataArray count]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { CellView *targetCustomCell = (CellView *)[tableView cellForRowAtIndexPath:indexPath]; [targetCustomCell checkAction:nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSMutableDictionary *selectedItem = [self.dataArray objectAtIndex:indexPath.row]; [selectedItem setObject:[NSNumber numberWithBool:targetCustomCell.checked] forKey:@"checked"]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCustomCellID = @"MyCellID"; CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID]; if (cell == nil) { cell = (CellView *)[[[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease]; } NSDictionary *item = [dataArray objectAtIndex:indexPath.row]; NSString* title = [item objectForKey:@"text"]; cell.title = title; cell.textLabel.text = title; cell.checked = [[item objectForKey:@"checked"] boolValue]; return cell; } - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { // called when the accessory view (disclosure button) is touched CellView *cell = (CellView *)[tableView cellForRowAtIndexPath:indexPath]; Cell_TouchAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys: cell.title, @"text", [NSNumber numberWithBool:cell.checked], @"checked", nil]; [appDelegate showDetail:infoDict]; } |
Step 11: Compile and run the application in the simulator.
You can downloaded SourceCode from here Cell Touch 2
Implementing ListView With Search
Implementing “Rate This App” Feature
Implementing Asynchronous Image Loading In Listview
Implementing Dashboard Design In Your Application
Drag and Drop Details and Implementation in Android
Text To Speech In iPhone
Updating A Progress Bar (iPhone)
Simple Threading In iPhone