Taking contact information and putting it into a UITableView - objective-c

I am creating an app where you press a button and it opens up your contacts list. You can then select the contact you want to add and it imports their name and email into the app. I currently have that information going into labels but I want to add it to a table view cell. How would I do this?
My Code:
.h:
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
#interface FirstViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
- (IBAction)showPicker:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *firstName;
#property (weak, nonatomic) IBOutlet UILabel *email;
#end
.m:
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize firstName;
#synthesize email;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName.text = name;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".
self.email.text = emailId;
}
#end

OK, I am going to explain how you programmatically implement a very basic table view controller. It will be up to you, though, to figure out how to integrate this into your application.
Let's start with the header file, let's call it MyTableViewController.h:
#interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
}
#end
As you can see, your controller class adopts the protocols UITableViewDelegate and UITableViewDataSource.
Now let's look at a first snippet from the implementation file MyTableViewController.m. Your first job, obviously, is to create the controller's view. You do this in your controller's loadView method. If you want to learn more about the view life cycle and how to program a UIViewController I suggest you read the UIViewController class reference and the accompanying View Controller Programming Guide.
- (void) loadView
{
// Give the view some more or less arbitrary initial size. It will be
// resized later when it is actually displayed
CGRect tableViewFrame = CGRectMake(0, 0, 320, 200);
UITableView* tableView = [[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStyleGrouped] autorelease];
self.view = tableView;
// Here we make sure that the table view will take as much horizontal
// and vertical space as it can get when it is resized.
UIViewAutoresizing autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tableView.autoresizingMask = autoresizingMask;
// We need to tell the table view that we are both its delegate and
// its data source.
tableView.delegate = self;
tableView.dataSource = self;
}
Just to let you know: You can omit loadView entirely if your controller is a subclass of UITableViewController, but I deliberately do not take that shortcut so that I can show you how a table view needs a delegate and a data source. Most important ist the data source.
In the next snippet in MyTableViewController.m we are going to implement some basic UITableViewDataSource methods. For this you need to understand how a table view is structured: A table view is divided into sections, and each section has a number of cells. The point of having sections is to visually separate groups of cells, with an optional section header or footer. I am not going into details here, though, to keep this simple.
- (NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
// Let's keep it simple: We want just one section
return 1;
}
- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
// Let's keep it simple: We want just one row, or table view cell.
// Since we only have one section (see above) we don't have to look
// at the section parameter.
return 1;
}
And now, finally, the centerpiece where you create your table view cell. Again, this is a UITableViewDataSource method that we implement. Note that we do not need to inspect the indexPath parameter only because we know that we only have one section and one row. In a real world application you will probably have to write switch-case or if-else statements that examine indexPath.section and indexPath.row so that you can distinguish between the different cells you need to create.
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// This is very important for your future table view implementations:
// Always ask the table view first if it already has a cell in its
// cache. If you don't do this your table view will become slow when
// it has many cells.
NSString* identifier = #"MyTableViewCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
// Aha, the table view didn't have a cell in its cache, so we must
// create a new one. We use UITableViewCellStyleValue1 so that the
// cell can display two pieces of information.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
}
// Regardless of whether we got the cell from the table view's cache
// or create a new cell, we must now fill it with content.
// First, obtain the information about the person from somewhere...
NSString* personName = ...;
NSString* personEmail = ...;
// ... then add the information to the table cell
cell.textLabel.text = personName;
cell.detailTextLabel.text = personEmail;
return cell;
}
As a final nicety, we implement a UITableViewDelegate method:
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Here you can react to the user tapping on the cell. If you
// don't want the user to be able to select a cell you can
// add the following line to tableView:cellForRowAtIndexPath:
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
It is difficult to tell how you should integrate this into your application. It all depends where you want to display the table view. Since you say you want to replace the two labels you already have, one possible approach could be this:
In Interface Builder, add the table view as a subview to the main view of your FirstViewController
Add an outlet to FirstViewController that you connect to the table view
Let FirstViewController adopt the protocols UITableViewDelegate and UITableViewDataSource
Connect FirstViewController to the delegate and data source outlets of the table view
Don't implement loadView from my example, you don't need it, you already have made all the connections etc. in Interface Builder
If you need further help with integration, I suggest that you ask a new question and possibly refer to this answer. Good luck.

Related

NSTableView is not being displayed

This is a follow-up on the previous question.
Sorry. I could not figure out how to add code or edit something written over 5 minues ago.
A brief summary. I am trying to display a customized/derived TableView over a regular View. I am not using IB, but doing everything from the code. The goal here is to build the application, but also to learn Cocoa/OSX programming. This is my first OSX coding attempt.
NSView atop of which I would like to display my custom TableView is being displayed fine. Please excuse the NSLog garbage. It helps me to learn about the app lifecycle.
Header:
#import <Cocoa/Cocoa.h>
#import "MSNavigationTableView.h"
#interface MSNavigationPanelView : NSView
#property (strong, nonatomic) IBOutlet MSNavigationTableView *myNavigationTable;
#end
code:
#import "MSNavigationPanelView.h"
#implementation MSNavigationPanelView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
NSLog(#"Initializing Navigation Panel");
}
self.myNavigationTable = [[MSNavigationTableView alloc] initWithFrame:self.frame];
[self.myNavigationTable setDataSource:self.myNavigationTable];
[self.myNavigationTable setDelegate:self.myNavigationTable];
[self addSubview:self.myNavigationTable];
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
NSLog(#"Drawing navigation view!");
}
#end
Now the NSTableView derived class.
Header:
#import <Cocoa/Cocoa.h>
#interface MSNavigationTableView : NSTableView <NSTableViewDataSource>
#end
NSArray *myNavigationArray;
Source:
#import "MSNavigationTableView.h"
#implementation MSNavigationTableView
+ (void)initialize {
NSLog(#"Called NavigationTableView::initialize!");
myNavigationArray = [NSArray arrayWithObjects:#"Call History" #"Contacts", #"Messages", #"Voicemail", nil];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
}
- (NSInteger)numberOfRowsInTableView: (NSTableView *) aTableView
{
return [myNavigationArray count];
}
- (id)tableView: (NSTableView*) aTableView objectValueForTableColumn: (NSTableColumn *)aTableColum row: (NSInteger)rowIndex
{
NSLog([myNavigationArray objectAtIndex:rowIndex]);
return [myNavigationArray objectAtIndex:rowIndex];
}
#end
Thank you. I am sure that I am doing something stupid, and/or perhaps not doing something necessary. I have tried to figure this out for a couple of hours. No ideas so far.
You really need to use Interface Builder to make a table.
I would never try to programmatically initialized a table... to many things to configure.
NSTableView needs to have NSTableColumns, NSTableColumns need to have NSCell's, etc. etc.
NSTableView needs to be embedded in an NSScrollView.
I figured out what needs to be done.
First, array initialization has to be moved from +(void)initialize to another method. For me - (id)initWithFrame works fine.
Second, while this was not clear for me, overwriting NSTableViewDataSource is not enough.
One has to create NSTableColumn(s) then add the column(s) to the table using addTableColumn method of NSTableView class. Once that is done, we proceed with setDataSource and so on.

viewdidload issue xcode 4.2

i just learn how to change switch between views with push and pop.
now, to my second view i add a label witch i wand to change her value every time my second view is push.
i add the label, connect her to my file owner's and i use viewdidload to change her value.
when i entered to my second view nothing is happed. but when i use viewdidapper all work perfect(but it take a second until the label value is update).
my code is:
mysecondviewcontroller.h:
#interface SecondViewController : UIViewController
{
IBOutlet UILabel *textLabel;
NSString *label;
}
#property (copy) NSString *label;
#end
mysecondviewcontroller.m(ofcourse i synthesize label):
-(void)viewDidAppear:(BOOL)animated
{
textLabel.text = label;
NSLog(#"viewdidapper2");
}
- (void)viewDidLoad
{
textLabel.text = label;
[super viewDidLoad];
NSLog(#"viewdidload2");
// Do any additional setup after loading the view from its nib.
}
my firstviewcontroller.m(IBAction):
- (IBAction)pushViewController:(id)sender
{
static int count = 1;
SecondViewController *secondVieController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVieController animated:YES];
secondVieController.title = #"second";
secondVieController.label = [NSString stringWithFormat:#"number: %d", count];
count++;
}
what is the problem in my viewdidload?
thanks!
If you're using viewDidLoad, you need to call the super function before doing anything else.
- (void)viewDidLoad
{
[super viewDidLoad];
textLabel.text = label;
NSLog(#"viewdidload2");
// Do any additional setup after loading the view from its nib.
}
I think that there is another issue, you are setting secondVieController.label after pushing the view controller, but this means at the time that viewDidLoad runs, secondVieController.label is still empty. This should fix it.
- (IBAction)pushViewController:(id)sender
{
static int count = 1;
SecondViewController *secondVieController = [[SecondViewController alloc] init];
secondVieController.title = #"second";
secondVieController.label = [NSString stringWithFormat:#"number: %d", count];
[self.navigationController pushViewController:secondVieController animated:YES];
count++;
}
If you Want to Update Label Every Time when View Loaded Then You have to Write the code in to Viewwillappear Method.

editButtonItem Not Showing Up

The Problem:
The built-in editButtonItem that Xcode automatically comments out when a new UITableViewController class is created does not work when I delete the comment slashes (//). By does not work I mean that the edit button does not appear at all. Swiping a cell does not work either.
Attempted Solutions:
I have tried to follow the various workarounds that have been posted on other stackoverflow threads to no avail. Most of the posts that I have found talk about various aspects of the edit button not working (e.g., no minus signs showing up, etc…) but very few that I have found in which the edit button does not show up at all.
Hunch:
I have a hunch that it might have something to do with the UITableViewController not being properly implemented. I am very new to object-oriented programming as well as objective-c, so I apologize if the answer is something very basic—but hey, it's part of the learning process. Any help is much appreciated.
Code:
____.h
#import <UIKit/UIKit.h>
#import "IndividualRecipeViewController.h"
#class BrowsePrivateRecipeViewController;
#protocol BrowsePrivateRecipeViewControllerDelegate
- (void)browsePrivateRecipeViewControllerDidFinish:(BrowsePrivateRecipeViewController *)controller;
#end
#interface BrowsePrivateRecipeViewController : UITableViewController
#property (weak, nonatomic) id <BrowsePrivateRecipeViewControllerDelegate> delegate;
#property (assign, nonatomic) NSUInteger listLength;
#property (strong, nonatomic) NSDictionary *dictionaryOfRecipes;
#property (strong, nonatomic) NSMutableArray *arrayOfRecipeNames;
// ... methods
#end
____.m
#interface BrowsePrivateRecipeViewController ()
#end
#implementation BrowsePrivateRecipeViewController
#synthesize delegate = _delegate;
#synthesize listLength = _listLength;
#synthesize dictionaryOfRecipes = _dictionaryOfRecipes;
#synthesize arrayOfRecipeNames = _arrayOfRecipeNames;
- (void)viewDidLoad
{
// ... code here
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// ... other methods
UPDATE:
LINK TO SOURCE CODE
So I have decided to post the source code to my whole project. I am having this problem with multiple files, but if I get it fixed in one, I am pretty sure that the rest will fall into place.
Please focus on the files BrowsePrivateRecipeViewController.m/.h. This is the most straightforward instance of the problem.
Once again thank you for your patience and help.
Sincerely,
Jason
First of all, I would definately not use a custom button for editing the table. It's unnecessary simply because there's already one built in.
Just use UIViewControllers editButtonItem.
If you have to perform additional stuff on button press, override -setEditing:animated: and call super first.
The error you mentioned above is caused because you're trying to access the navigationBars navigationItem, which does not exist. You should access your view controller's navigationItem.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
You need to make a button first. This will make an Edit button then add it to the rightBarButtonItem spot.
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
self.navigationItem.rightBarButtonItem = editButton;
You then need to set up a method to turn on the table's edit mode.
- (void)editTable
{
[self.tableView setEditing:YES animated:YES];
}
Update:
Just read your question again and noticed you want swipe to delete as well. You need to added these methods in order to add that to your tableview.
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Update 2
__.h
#property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
__.m
#synthesize navigationBar;
- (void)viewDidLoad {
//...
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
self.navigationBar.navigationItem.rightBarButtonItem = editButton;
}
You aren't alloc'ing or init'ing the editButtonItem object, so how can you expect to retain it (equals sign), let alone have it show up? You're basically sending a message to nil.

Linking Custom NSButton to View Based Table Row and Action

I have a view based table with a custom cell view. Inside this custom cell view, I have a custom NSButton that acts like a check box (it toggles a custom image on and off). This part works well. The images toggle on and off perfectly.
What I want to do is associate the custom button in the row with the actual row in the table. When I check/click the button it will highlight the the corresponding row and then perform an action on the row in which the check box/ button is situated. For example, removing the row from the table when the NSButton is clicked.
My custom NSButton is implemented as follows:
Header file:
#import <Foundation/Foundation.h>
#interface CustomCheckButton : NSButton {
BOOL _checked;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
Implementation:
#import "CustomCheckButton.h"
#implementation CustomCheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
self.checked = NO;
[self setTarget:self];
[self setAction:#selector(onCheck:)];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self setTarget:self];
[self setAction:#selector(onCheck:)];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
NSImage* img = [NSImage imageNamed:#"check_on.png"];
[self setImage:img];
[self setState:NSOnState];
}
else
{
NSImage* img = [NSImage imageNamed:#"check_off.png"];
[self setImage:img];
[self setState:NSOffState];
}
}
-(void) onCheck:(id) sender
{
self.checked = !_checked;
NSLog(#"A check box was pressed");
}
#end
The current solution does not associate the button with the row at all. When I sort the rows, for example, the selected image is not linked to the row and often stays in the same place within the table, even though a different row was selected.
Any help on this would be greatly appreciated.
How about adding a delegate field and setting it with a custom initializer? Or you could add it as a property and set it after calling alloc init. This can be done in 2 ways: via IB or programmatically.
If you want to do it programmatically, you'll need to do a couple of things:
So let's look at the class that has a UITableView element in it. Assuming this class is also the UITableViewDelegate and UITableViewDataSource, it will have to have an implementation of the data source method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
which returns a cell at a given indexPath. Here, you could instantiate your custom cell and in that, add a custom button and set it's delegate to the cell itself, by adding this to your code:
in CustomCheckButton.h:
UITableViewCell * delegate;
//...
#property (nonatomic, assign) UITableViewCell * delegate;
in CustomCheckButton.m
#synthesize delegate;
in the table view data source method:
// initialize my custom cell element, assume it has a property for a CustomCheckButton with name checkButton
// ... some code
myCell.checkButton.delegate = mycell; // link the button's delegate to the cell
// ... some other code, probably based on indexPath
return myCell;
This might seem like a big task, but you can do it in interface builder too, assuming you have a xib for your custom cell class. If you do, just open the xib. You'll probably have a button on it already; change it's class to your own CustomCheckButton and you'll be able to set its delegate property like you would normally, with ctrl-drag from the button to the cell.
Hope this helps!

NSMutableArray Returns NULL

I try adding objects to NSMutableArray from another class (secondViewController) and then add it to my UITableView in my FirstViewController, but it returns null when I print it using NSLog. Here is my set up.
FirstViewController.h:
#interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *mytableview;
NSMutableArray *mytableinfo;
}
#property (nonatomic,retain) IBOutlet UITableView *mytableview;
#property (retain) IBOutlet NSMutableArray *mytableinfo;
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#synthesize mytableinfo,mytableview;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentModalViewController:secondViewController animated:YES];
}
- (void)viewDidLoad
{
mytableinfo = [[NSMutableArray alloc]init];
[super viewDidLoad];
}
SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
#implementation SecondViewController
#synthesize dateformatter,mydatepicker,startingTime;
-(IBAction)saveShift:(id)sender{
FirstViewController *firstViewController = [[FirstViewController alloc]init];
[firstViewController.mytableinfo addObject:#"Hello world"];
NSLog(#"%#",firstViewController.mytableinfo);
[self dismissModalViewControllerAnimated:YES];
}
My goal is to ultimately feed a mytableviewfrom mytableinfo. I'm not even sure if this is the best way to go about it. Any advice would be appreciated.
In SecondViewController, you are creating a FirstViewController with alloc init. At that point, mytableinfo on FirstViewController is nil because you don't allocate until viewDidLoad.
What loads SecondViewController? Because you're dismissing it modally. If it's FirstViewController, then when you alloc init first view controller, you're not calling the instance that presented it modally.
It's also not very MVC to have one view poke at another like that. It creates code that's couple at the view layer and modifying data at the view layer. It is better to create a model and have both views modifying that model.
How to create a NSMutable Array which can access from different view controllers
Another way to communicate between views is for one view to pass a delegate (a callback) to the other view. That allows the other view to not be coupled to the other view - it only knows about the protocol for the delegate.
What exactly does delegate do in xcode ios project?
There is a point that look strange to me in your "SecondViewController" you dissmiss it like it's a modal.
My Question is then... who started the modal presentation?
A "FirstViewController"? If it's the case, why are you creating a new one, on dismissing the second, the First that launched it will resume it's activity.
An other thing that I don't understand is that the designated initializer for a UIViewController is
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
You can pass nil to both argument, if no nib need to be associated with.
And finaly, if you need to get back a NSMutableArray to a 1st ViewController (VC) from a 2nd VC that was modally presented by the 1st you can do this in the 2nd VC:
- (id)initWithMutableArray:(NSMutableArray *)theArray {
//... put standard init code here }
And make that the default initializer of your second VC. But this make sense only if 2nd VC absolutely need a mutable array.
And now for my curiosity because I don't understand this line
#property (retain) IBOutlet NSMutableArray *mytableinfo;
Why is this an IBOutlet? That look like a potential source of problem.
IBOutlet are usually pointers to UI elements in a xib file.
When populating a UITableView with an array that can be modified by multiple modal views during the course of your app, I find one of the best ways to do this is with NSUserDefaults. You can create an NSUserDefaults object for reference like this:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
Then you can assign objects to each key in defaults, which is really just a plist (which is just a list of keys with objects associated with them.
So then, when you want to store the array in defaults, you can say:
[defaults setObject:mytableinfo forKey:#"tableInformationKey"];
Then, whenever you want to access that data, you can say:
NSMutableArray* tableInfoCopy = [defaults mutableArrayValueForKey:#"tableInformationKey"];
That will make you a copy of the array you have stored in NSUserDefaults (NSUserDefaults can be accessed from anywhere in your app), so then you can make changes to that mutable array you just made. Once you are done making changes, you can reassign it to NSUserDefaults like this:
[defaults setObject:tableInfoCopy forKey#"tableInformationKey"];
So when you populate your UITableView, in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
put something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Foobar"];
if (cell == nil) {
// No cell to reuse => create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Foobar"] autorelease];
// Initialize cell with some customization
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
NSArray* arrayOne = [defaults objectForKey:#"tableInformationKey"];
NSString* title = [arrayTwo objectAtIndex:indexPath.row];
//this goes to the index in the array of whatever cell you are
// at, which will populate your table view with the contents of this array (assuming the array contains strings)
// Customize cell
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:25];
return cell;
}
Use them the easiest way is to put the array in your AppDelegate
// populate appDelegate's array
AppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.arrayMyTableInfo addObject:#"HelloWorld"];
Here is how you can do what I've suggested :
(This is part of your code updated)
This is in your secondView, this is one of many way to pass the array to your second view.
#synthesize array4Test;
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
self.array4Test = aMutableArray;
}
return self;
}
- (void)dealloc
{
// HERE clean up the property is set to retain.
self.array4Test = nil;
[super dealloc];
}
Here is the code for the firstView
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
NSLog(#"This just came back from the second View\n%#", aString);
}
}