Pass CLLocation data to UITableView - ios

I'm trying to load my UITableView with distance from current location. I'm taking small steps in just trying to get the Latitude to load into the UITableView. My NSLog is reading the correct Lat/Long, but my Table is reading 0.000. At this point I'm not sure if it's my memory management or something else. Please help.
My ViewController.h
#import <UIKit/UIKit.h>
#import "CoreLocation/CoreLocation.h"
#interface TulsaMasterViewController : UITableViewController <CLLocationManagerDelegate>
{
NSArray *_barInfo;
CLLocationManager *lm;
NSString *currentLat;
}
#property (nonatomic, strong) NSArray *barInfo;
#property (nonatomic, strong) NSString *currentLat;
#end
My ViewController.m
#import "TulsaMasterViewController.h"
#import "TulsaDetailViewController.h"
#import "Bars.h"
#import "BarDatabase.h"
#implementation TulsaMasterViewController
#synthesize barInfo = _barInfo;
#synthesize currentLat = _currentLat;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%f", newLocation.coordinate.latitude);
NSLog(#"%f", newLocation.coordinate.longitude);
currentLat = [NSString stringWithFormat:#"%f", newLocation.coordinate.latitude];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *msg = [[NSString alloc]initWithString:#"Error obtaining location"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:msg delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.barInfo count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Get the object from the array.
Bars *barObj = [self.barInfo objectAtIndex:indexPath.row];
//Set the coffename.
cell.textLabel.text = barObj.barName;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%f", currentLat];
// Set up the cell
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
TulsaDetailViewController *detailViewController = [segue destinationViewController];
detailViewController.detailItem = [self.barInfo objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.barInfo = [BarDatabase database].barInfo;
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
[lm startUpdatingLocation];
}
#end

You need to update your tableview when the didUpdateToLocation method is called

Related

How show the data in UILabel from a NSMutableArray?

I have in my .h file:
#import <UIKit/UIKit.h>
#import "SQLClient.h"
#interface mgrViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,
SQLClientDelegate>{
NSMutableArray *pajaros;
}
#property (weak, nonatomic) IBOutlet UITableView *miTabla;
#property (nonatomic, retain)NSMutableArray *pajaros;
#end
And in my .m file:
#import "mgrViewController.h"
#import "Vista2.h"
#import "SQLClient.h"
#interface mgrViewController ()
#end
#implementation mgrViewController
#synthesize miTabla;
#synthesize pajaros;
- (void)viewDidLoad
{
[super viewDidLoad];
SQLClient* client = [SQLClient sharedInstance];
client.delegate = self;
[client connect:#"xxx.xxx.xxx.xxx:xxxx" username:#"xxxxxxxxxxx" password:#"xxxxxxxxxxxx" database:#"xxxxxxxxxxx" completion:^(BOOL success) {
if (success)
{
pajaros =[[NSMutableArray alloc]init];
[client execute:#"SELECT field FROM table WHERE field='xxxxxxxxx'" completion:^(NSArray* results) {
NSMutableString* resulta = [[NSMutableString alloc] init];
for (NSArray* table in results)
for (NSDictionary* row in table)
for (NSString* column in row){
//[results appendFormat:#"\n%# = %#", column, row[column]];
[resulta appendFormat:#"\n%#", row[column]];
[pajaros addObject:resulta];
}
[client disconnect];
}];
}
}];
self.miTabla.delegate = self;
self.miTabla.dataSource = self;
}
#pragma mark - SQLClientDelegate
- (void)error:(NSString*)error code:(int)code severity:(int)severity
{
NSLog(#"Error #%d: %# (Severity %d)", code, error, severity);
[[[UIAlertView alloc] initWithTitle:#"Error" message:error delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
- (void)message:(NSString*)message
{
NSLog(#"Message: %#", message);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return pajaros.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"celdaPajaros";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// elementos que contienen cada celda con sus tags
UILabel *labelTitulo = (UILabel *) [cell viewWithTag:10];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
labelTitulo.text = [pajaros objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70.f;
}
#end
If I add a count for my NSMutableArray pajaros after the line of code [pajaros addObject:resulta];and I print that count, the result is 1, because my conditional where is for select a data. But if put a count in other part of my code, the result is 0.
My question is how I retain the data in my NSMutableArray for use in:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return pajaros.count;
}
and in:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"celdaPajaros";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// elementos que contienen cada celda con sus tags
UILabel *labelTitulo = (UILabel *) [cell viewWithTag:10];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
labelTitulo.text = [pajaros objectAtIndex:indexPath.row];
return cell;
}
?
Thanks for the help, I'm new in Objective-C.
If I initialize pajaros with a data in:
- (void)viewDidLoad
{
[super viewDidLoad];
pajaros = [NSMutableArray arrayWithObjects:#"Bird1", nil];
self.miTabla.delegate = self;
self.miTabla.dataSource = self;
}
And run my app, the labelTitulo show me Bird1. My second question is: why when I add data to my NSMutableArray pajaros from a Data Base and run my app, it's show me nothing?
Thanks.
When using properties, it's best to include self.. Try self.pajaros = [[NSMutableArray alloc]init]; and likewise in the other places where you just use pajaros.

How i use a NSMutableArray in two methods of my .m file?

I have in my .h file:
#import <UIKit/UIKit.h>
#import "SQLClient.h"
#interface mgrViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,
SQLClientDelegate>{
NSMutableArray *pajaros;
}
#property (weak, nonatomic) IBOutlet UITableView *miTabla;
#property (nonatomic, retain)NSMutableArray *pajaros;
#end
And in my .m file:
#import "mgrViewController.h"
#import "Vista2.h"
#import "SQLClient.h"
#interface mgrViewController ()
#end
#implementation mgrViewController
#synthesize miTabla;
#synthesize pajaros;
- (void)viewDidLoad
{
[super viewDidLoad];
SQLClient* client = [SQLClient sharedInstance];
client.delegate = self;
[client connect:#"xxx.xxx.xxx.xxx:xxxx" username:#"xxxxxxxxxxx" password:#"xxxxxxxxxxxx" database:#"xxxxxxxxxxx" completion:^(BOOL success) {
if (success)
{
pajaros =[[NSMutableArray alloc]init];
[client execute:#"SELECT field FROM table WHERE field='xxxxxxxxx'" completion:^(NSArray* results) {
NSMutableString* resulta = [[NSMutableString alloc] init];
for (NSArray* table in results)
for (NSDictionary* row in table)
for (NSString* column in row){
//[results appendFormat:#"\n%# = %#", column, row[column]];
[resulta appendFormat:#"\n%#", row[column]];
[pajaros addObject:resulta];
}
[client disconnect];
}];
}
}];
self.miTabla.delegate = self;
self.miTabla.dataSource = self;
}
#pragma mark - SQLClientDelegate
- (void)error:(NSString*)error code:(int)code severity:(int)severity
{
NSLog(#"Error #%d: %# (Severity %d)", code, error, severity);
[[[UIAlertView alloc] initWithTitle:#"Error" message:error delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
- (void)message:(NSString*)message
{
NSLog(#"Message: %#", message);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return pajaros.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"celdaPajaros";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// elementos que contienen cada celda con sus tags
UILabel *labelTitulo = (UILabel *) [cell viewWithTag:10];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
labelTitulo.text = [pajaros objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70.f;
}
#end
I added elements to NSMutableArray pajaros in -(void)viewDidLoad{}, and my question is, how I can use my NSMutableArray pajaros in -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath?.
Thanks :)
If I use count on my NSMutable Array and print that number, the result is 1 (because I selected only A data), but only if I use:
...
[pajaros addObject:resulta];
}
NSInteger num = pajaros.count;
NSString *inStr = [NSString stringWithFormat: #"%ld", (long)num];
self.textView.text = inStr;
[client disconnect];
}];
...
But if I perform that count in another part of my .m file show me 0, my next questions becomes why my NSMutableArray contains only the data in:
...
[pajaros addObject:resulta];
}
[client disconnect];
}];
...
?
Thanks for the help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//code here to dequecell
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell.textLabel.text = [pajaraos objectAtIndex:indexPath.row];
}
return cell;
}

How to proper initialize an object? (CLLocation in my case)

I am trying to fill my tableView with a points and distances to current location.
I've got a problem with initialize property.
In .h file:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface SIBViewController : UIViewController
<CLLocationManagerDelegate>
{
NSArray *_data;
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
#property (nonatomic, retain) CLLocation *currentLocation;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
In .m file:
#import "SIBViewController.h"
#import "atmCell.h"
#import "sibAtmData.h"
#interface SIBViewController ()
#end
#implementation SIBViewController
#synthesize currentLocation;
- (void)viewDidLoad
{
[super viewDidLoad];
[self startSignificantChangeUpdates];
_data = [sibAtmData fetchData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"atmCell";
atmCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
sibAtmData *item = [_data objectAtIndex:indexPath.row];
cell.titleLabel.text = item.title;
cell.subtitleLabel.text = item.subtitle;
CLLocationDistance distance = [self.currentLocation distanceFromLocation:item.location];
cell.distanceLabel.text = [NSString stringWithFormat:#"%.1f km", distance/1000];
NSLog(#"distance: %f", distance);
return cell;
}
- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// If it's a relatively recent event, turn off updates to save power
self.currentLocation = [locations lastObject];
NSDate* eventDate = currentLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
[self.tableView reloadData];
// If the event is recent, do something with it.
NSLog(#"latitude %+.6f, longitude %+.6f\n",
currentLocation.coordinate.latitude,
currentLocation.coordinate.longitude);
}
}
#end
But currentLocation is empty:
currentLocation CLLocation * 0x00000000
I've tried to write in viewDidLoad:
currentLocation = [[CLLocation alloc] init];
but this didn't help me.
Memory allocates for object, but object creates without _latitude and _longitude properties
What am I doing wrong?

Sort TableView by distance from current location

I have an SQL DB that contains Lat and Long info. I have found my current location and been able to get the distance of each location from my current location. I can get my tableview to show this distance, but know I want to sort that tableview so the closest are listed first.
My thought is I will need to add the distance to my SQL DB data, sort that some how and then display that info back to the tableview.
My thought is I would do all of this within my TableView. Looking for guidance on how to do this and if I should be doing it in the tableview.
#import "TulsaMasterViewController.h"
#import "TulsaDetailViewController.h"
#import "Bars.h"
#import "BarDatabase.h"
#implementation TulsaMasterViewController
#synthesize barArray = _barArray;
#synthesize currentLat = _currentLat;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
currentLat = newLocation;
if (newLocation.horizontalAccuracy <= 100.0f) {
[lm stopUpdatingLocation];
}
[self.tableView reloadData];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *msg = [[NSString alloc]initWithString:#"Error obtaining location"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:msg delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.barArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Get the object from the array.
Bars *barObj = [self.barInfo objectAtIndex:indexPath.row];
//Set the name.
cell.textLabel.text = barObj.barName;
if (currentLat == nil) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"?"];
}else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"%.02f", cachedDist];
}
// Set up the cell
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
TulsaDetailViewController *detailViewController = [segue destinationViewController];
detailViewController.detailItem = [self.barArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.barArray = [BarDatabase database].barInfo;
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
[lm startUpdatingLocation];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
for (Bars *barObj in barArray) {
NSString *strLat = barObj.Lat;
NSString *strLong = barObj.Long;
CLLocation *barLocation = [[CLLocation alloc] initWithLatitude:[strLat doubleValue] longitude:[strLong doubleValue]];
CLLocationDistance distance = [currentLat distanceFromLocation:barLocation]/1000;
[barArray addObject:[NSNumber numberWithDouble:distance]];
NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:#"cachedDist" ascending:YES];
[barArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}
For reference the rest of my code
Bars.h
#import <Foundation/Foundation.h>
#interface Bars : NSObject {
NSString *barName;
NSString *barAddress;
NSString *Lat;
NSString *Long;
NSString *cachedDist;
}
#property (nonatomic, copy) NSString *barName;
#property (nonatomic, copy) NSString *barAddress;
#property (nonatomic, copy) NSString *Lat;
#property (nonatomic, copy) NSString *Long;
#property (nonatomic, copy) NSString *cachedDist;
- (id)initWithName:(NSString *)name address:(NSString *)address latitude:(NSString *)latitude longitude:(NSString *)longitude distance:(NSString *)distance;
#end
Bars.m
#import "Bars.h"
#implementation Bars
#synthesize barName = _barName;
#synthesize barAddress = _barAddress;
#synthesize Lat = _Lat;
#synthesize Long = _Long;
#synthesize cachedDist = _cachedDist;
- (id)initWithName:(NSString *)name address:(NSString *)address latitude:(NSString *)latitude longitude:(NSString *)longitude distance:(NSString *)distance;
{
if ((self = [super init])) {
self.barName = name;
self.barAddress = address;
self.Lat = latitude;
self.Long = longitude;
self.cachedDist = distance;
}
return self;
}
#end
BarDatabase.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#interface BarDatabase : NSObject
{
sqlite3 *_database;
}
+ (BarDatabase *)database;
- (NSMutableArray *)barInfo;
#end
BarDatabase.m
#import "BarDatabase.h"
#import "Bars.h"
#implementation BarDatabase
static BarDatabase *_database;
+ (BarDatabase *)database {
if (_database == nil) {
_database = [[BarDatabase alloc] init];
}
return _database;
}
- (id)init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:#"TulsaBars"
ofType:#"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(#"Failed to open database!");
}
}
return self;
}
- (void)dealloc {
sqlite3_close(_database);
}
- (NSMutableArray *)barInfo {
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *query = #"SELECT * FROM TulsaBars";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 1);
char *addressChars = (char *) sqlite3_column_text(statement, 2);
char *latChars = (char *) sqlite3_column_text(statement, 8);
char *longChars = (char *) sqlite3_column_text(statement, 9);
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSString *address = [[NSString alloc] initWithUTF8String:addressChars];
NSString *latitude = [[NSString alloc] initWithUTF8String:latChars];
NSString *longitude = [[NSString alloc] initWithUTF8String:longChars];
Bars *info = [[Bars alloc]
initWithName:name address:address latitude:latitude longitude:longitude];
[retval addObject:info];
}
sqlite3_finalize(statement);
}
return retval;
}
#end
Hrm. There should be no sorting happening inside cellForRowAtIndexPath. It's too late in the game. If each cell is 1 bar, and the bars should be sorted by distance, I recommend achieving the sort in a lazy getter for your self.barInfo (if that's your list of bars).
I'm not sure what your model looks like, but if each bar object has a property for distance from whatever your reference point is, your sorting would look similar to what you have there.
This sorting would only happen the when the first cell is loaded (that's the "lazy" part) and then cached. So your cellForRowAtIndexPath: would just ask for the model object at it's indexPath's row, trusting that it is in the right order.
I'm not sure how much of this terminology you are familiar with, so feel free to ask clarifying questions and I will iterate on the answer.
EDIT after sharing more code:
I think you should add a #property to the bar called something like cachedDistance, and whenever you come onscreen (viewWillAppear), iterate over the bars and set their cached distance (using similar code to what you have in cellForRow...). Then implement a getter for self.barArray: -(NSArray *)barArray which essentially returns the barArray sorted using a sortDescriptor with the name of your cached distance property as it's key. This will simplify your cellForRow... code a lot.
You can then extend the logic to recalculate the distances when you get location updates, perhaps only if it is a certain distance from the previous.

Add view from table view cell

I know this question has been asked before but mine is different. My app has an add button and edit button that deletes/adds table views. I want every cell that is created by the user to go to the same view. I've been looking everywhere for the code but I can't find it. BTW the ____ is just a placeholder. The table coding is in the app delegate and I have a second view controller for the view that is loaded when a row is clicked.
AppDelegate.h
#interface _____AppDelegate : NSObject <UIApplicationDelegate> {
CustomCellViewController *customCellViewController;
IBOutlet UIWindow *window;
IBOutlet UITableViewCell *customCell;
NSMutableArray *data;
IBOutlet UITableView *mainTableView;
IBOutlet UINavigationItem *navItem;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#property (nonatomic, retain) CustomCellViewController *customCellViewController;
- (IBAction)addRowToTableView;
- (IBAction)editTable;
- (NSString *)dataFilePath;
#end
AppDelegate.m
#import "______AppDelegate.h"
#implementation ______AppDelegate;
#synthesize window;
#synthesize navController=_navController;
#synthesize customCellViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
if (archivedArray == nil) {
data = [[NSMutableArray alloc] init];
} else {
data = [[NSMutableArray alloc] initWithArray:archivedArray];
}
// Override point for customization after application launch
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)addRowToTableView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Product" message:#"What is the name of your product?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert addTextFieldWithValue:#"" label:#"Name of product..."];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeURL;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UITextField *tf = [alert textFieldAtIndex:0];
[data addObject:tf.text];
[self saveData];
[mainTableView reloadData];
}
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self saveData];
[mainTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:"Cell"] autorelease];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)dataFilePath {
NSString *dataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
dataFilePath = [[documentDirectory stringByAppendingPathComponent:#"applicationData.plist"] retain];
return dataFilePath;
}
- (void)saveData {
[NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[data objectAtIndex:fromIndexPath.row] retain];
[data removeObject:item];
[data insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)dealloc {
[window release];
[_navController release];
[customCellViewController release];
[super dealloc];
}
#end
I don't mean to be harsh, but you have a lot of basics to learn. ARC will make your life much easier and your code better, by eliminating the need to manually manage memory (for the most part). You can enable it when you first start a project. You should.
Why is an App Delegate managing a table view? No, no no. The app delegate is supposed to respond to system-level events, not run your whole application. You need a separate view controller. Find some tutorials around the web and see how a basic app using a table view is structured. There are many. My favorites are on raywenderlich.com

Resources