# TableView NSArray Help

Discussion in 'Mac Programming' started by metalmaniac, Dec 14, 2010.

1. ### metalmaniac macrumors newbie

#1
Hey all,
i need some help, i hope this is the right place to ask,

I need help creating a small app that has an interface which allows you to choose one variable, and then displays a table view using a function of that variable
all code is explained in the the // and should hopefully explain what im trying to do
I am unsure as how to enter the angleBtheta and bellInequailty in a 2 column Tableview in the XIB
Code:
```// bellgraph.m
//Imagine 3 polarisers A, fixed at angle 0, then B, C at angles to A, where the angle of C is a multiple of the angle B
#import "bellgraph.h"
#import "math.h"
#define PI 3.14159265
@implementation bellgraph
- (IBAction)calculateBellInequalities:(id)sender
{
NSNumber *multiplier=[NSNumber numberWithFloat:[multiplierField floatValue]]; //Grab the multiplier from XIB
int i;
for(i=0;i<181;i++){ //start a loop
float angleBtheta; //Angle of B
angleBtheta=i; //Give angle B values increasing 0,1,2,3,4,...,180)
NSNumber *angleCtheta=[NSNumber numberWithFloat:angleBtheta*[multiplier floatValue]]; //Angle C = multiplier * angleB
NSNumber *cosAB,*cosAC,*cosBC; // defined by cosAB=cos(angle between A and B),
NSNumber *absoluteValue,*bellInequality;
absoluteValue=[NSNumber numberWithFloat:([cosAB floatValue]-[cosAC floatValue])];
//the absolute value is for the absolute value of cosAB-CosAC (should be unsigned but cant figure out how
bellInequality=[NSNumber numberWithFloat:([absoluteValue floatValue]-[cosBC floatValue]-1)];
//bellInequality = |cosAB−cosAC| + (cosBC) −1
NSArray *bellArray=[NSArray arrayWithObjects:bellInequality,nil];
//Add each value of bellInequality and angleBtheta to tableview with B a function of angleBtheta
}
}
-(int)numberOfRowsInTableView:(NSTableView *) tableView {
return [bellArray count];
}
@end```
and then my class

Code:
```// bellgraph.h
#import
@interface bellgraph : NSObject {
IBOutlet id multiplierField;
IBOutlet id tableView;
NSArray *bellArray;
}
- (IBAction)calculateBellInequalities:(id)sender;
@end```
Could someone please point me in the right direction
I have searched my entire uni for someone to teach me cocoa and failed so this is my last place to turn.
Ash

2. ### Matthew Yohe macrumors 68020

Joined:
Oct 12, 2006
#2
"I am unsure as how to enter the angleBtheta and bellInequailty in a 2 column Tableview in the XIB"

Not sure what you mean? You want to enter a number in the table view by hand? You want to fill in a textfield and use that number to calculate?

Also, I would recommend parting from the calculations and just try to setup a toy example. Then when you figure that out, stick your calculations in.

If you could be a little more clear about what you want to do, and maybe even post a picture of your interface.

3. ### metalmaniac thread starter macrumors newbie

#3
ok, unfortunately this is all quantum mechanics, (im a physics student),

i suppose all i really need help with is how take a simple function like y=mx and put the values in a table with a column of x and y,

i am fully able to set text fields and buttons, i am literally only stuck on populating the NSArray with the results of a for loop and connecting this to IB

thanks

4. ### Matthew Yohe macrumors 68020

Joined:
Oct 12, 2006
#4
No need to dumb down the calculations for me, I'm saying dumb them down for you.

There's no need to have any of that in there until you can actually populate a tableview.

You're pretty far off, I'd suggest starting here: http://borkware.com/quickies/one?topic=NSTableView

Also you can use M_PI for your pi constant.

5. ### metalmaniac thread starter macrumors newbie

#5

but i am completely stuck. all the documentation i find is just impossible to understand, or always in reference to ios programming.i guess im asking can someone please explain to me how to populate a table

I have reduced the problem to this

Code:
```//array.m

#import "array.h"

@implementation array

- (IBAction)makeArrays:(id)sender
{
int x,y;
NSNumber *multiplier=[NSNumber numberWithFloat:[mField floatValue]];

for (x=0;x++;x<181)
{
y=[multiplier floatValue]*x;

NSNumber *xValue = [NSNumber numberWithInt:x];
NSNumber *yValue = [NSNumber numberWithInt:y];

NSArray  *xArray = [NSArray arrayWithObject:xValue];
NSArray  *yArray = [NSArray arrayWithObject:yValue];
}
}

@end

```
Code:
```//array.h

#import <Cocoa/Cocoa.h>

@interface array : NSObject {
IBOutlet id mField;
}
- (IBAction)makeArrays:(id)sender;

@end```
this as far as i can tell creates 2 arrays, one with x, one with y. and thats it. I cant even test it.

I know it's a tall request, but ive been trying and trying, and it would be pointless to show all the methods ive used, theyve all failed.

If theres tutorials out there for how to populate your table with images, how to add values through the interface, add then export to csv, why is it so impossible to find an example of this.

ive read through macresearch, ive searched through forums, ive been on stack overflow and the apple dev docs. please can someone help me.

6. Dec 15, 2010
Last edited: Dec 15, 2010

### ianray macrumors 6502

Joined:
Jun 22, 2010
Location:
@
#6
Yes you can! It is always possible to take a step back, and build a test-harness.

There were a number of issues with the code you posted, including:
• Syntax of the 'for' loop
• Scope of xArray and yArray
• Size of xArray and yArray

This sample is by no means perfect, but hopefully it is a useful example. Paste into 'myarray.m' and compile from the command-line (in Terminal) or paste into a new XCode project if you prefer.

Code:
```// gcc -W -Wall -framework Foundation myarray.m
#import <Foundation/Foundation.h>

@interface MyArray : NSObject {
NSMutableArray* elements;
}

- (void)build;
- (void)dump;

@end

@implementation MyArray

const unsigned kArrayElements = 8;

- (void)build {
elements = [[NSMutableArray arrayWithCapacity:kArrayElements] retain];
unsigned x;
for (x = 0; x < kArrayElements; x++) {
}
}

- (void)dump {
unsigned x;
for (x = 0; x < kArrayElements; x++) {
NSLog(@"elements[%u] = %u",
x, [[elements objectAtIndex:x] unsignedIntValue]);
}
}

- (void)dealloc {
[elements release];
[super dealloc];
}

@end

int main(void) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

MyArray* array = [[MyArray alloc] init];
[array build];
[array dump];
[array release];

[pool drain];
return 0;
}```
Hope this helps