TableView NSArray Help

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

  1. metalmaniac macrumors newbie


    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
    // 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 *angleBrad=[NSNumber numberWithFloat:angleBtheta*PI/180]; //Convert Angles to rads from degrees
    NSNumber *angleCrad=[NSNumber numberWithFloat:[angleCtheta floatValue]*PI/180];
    NSNumber *cosAB,*cosAC,*cosBC; // defined by cosAB=cos(angle between A and B),
    cosAB=[NSNumber numberWithFloat:cos([angleBrad floatValue])]; //cos(B)
    cosAC=[NSNumber numberWithFloat:cos([angleCrad floatValue])]; //cos(C)
    cosBC=[NSNumber numberWithFloat:cos(([angleCrad floatValue])-([angleBrad floatValue]))]; //cos(C-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];
    and then my class

    // bellgraph.h
    @interface bellgraph : NSObject {
    IBOutlet id multiplierField;
    IBOutlet id tableView;
    NSArray *bellArray;
    - (IBAction)calculateBellInequalities:(id)sender;
    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.
    Thanks in advance,
  2. Matthew Yohe macrumors 68020

    Oct 12, 2006
    "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


    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

  4. Matthew Yohe macrumors 68020

    Oct 12, 2006
    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:

    Also you can use M_PI for your pi constant.
  5. metalmaniac thread starter macrumors newbie


    cool, didnt know about M_PI,

    thanks for the reply,
    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

    #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];
    #import <Cocoa/Cocoa.h>
    @interface array : NSObject {
    IBOutlet id mField; 
    - (IBAction)makeArrays:(id)sender;
    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. ianray, Dec 15, 2010
    Last edited: Dec 15, 2010

    ianray macrumors 6502

    Jun 22, 2010
    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.

    // gcc -W -Wall -framework Foundation myarray.m
    #import <Foundation/Foundation.h>
    @interface MyArray : NSObject {
    	NSMutableArray* elements;
    - (void)build;
    - (void)dump;
    @implementation MyArray
    const unsigned kArrayElements = 8;
    - (void)build {
    	elements = [[NSMutableArray arrayWithCapacity:kArrayElements] retain];
    	unsigned x;
    	for (x = 0; x < kArrayElements; x++) {
    		[elements addObject:[NSNumber numberWithInt:x * 100]];
    - (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];
    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 :)

Share This Page