A. Hillegass Chapter 6, Challenge 2

Discussion in 'Mac Programming' started by MorphingDragon, May 7, 2010.

  1. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    So the challenge is to make a To Do List with a TextField, TableView and a MutableArray.

    I can add items to the array but I can't figure out how to update the TableView with the array contents. I'm pretty sure I've made the right connections in IB.

    This is my code so far.

    Code:
    //
    //  ToDoList.h
    //  ToDoList
    //
    //  Created by Robert Litchfield on 7/05/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import <Cocoa/Cocoa.h>
    
    
    @interface ToDoList : NSObject {
    	NSMutableArray *toDoList;
    	IBOutlet NSTableView *tableView;
    	IBOutlet NSTextField *textField;
    }
    
    -(IBAction)addEntry:(id)sender;
    -(id)init;
    -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
    -(int)numberOfRowsInTableView:(NSTableView *)tv;
    
    @end
    
    Code:
    //
    //  ToDoList.m
    //  ToDoList
    //
    //  Created by Robert Litchfield on 7/05/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import "ToDoList.h"
    
    
    @implementation ToDoList
    
    -(id)init {
    	[super init];
    	
    	toDoList = [[NSMutableArray alloc] init];
    	NSLog(@"Init Succesful");
    	return self;
    }
    
    -(IBAction)addEntry:(id)sender {
    	[toDoList addObject:[textField stringValue]];
    	NSLog(@"Entry Added");
    	
    	NSLog(@"Array now:");
    	for (NSString *ts in toDoList)
    		NSLog(@"%@", ts);
    	
    }
    
    -(int)numberOfRowsInTableView:(NSTableView *)tv {
    	return [toDoList count];
    }
    
    -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
    	NSString *c = [toDoList objectAtIndex:row];
    	return c;
    }
    @end
    
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    The table normally does not watch the datasource, you have to advise it when a change has occurred. Look up the documentation to find the appropriate method for that (I am not telling you the method outright, because you probably should get in the habit of having your documentation handy).
     
  3. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #3
    Man, can't believe I overlooked something as simple as reloadData.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No. Conceptually that doesn't even make sense: the delegate methods are there for the table view to call to influence it's behaviour once it wants to do something. You are trying to tell it to do something so it can't be calling a delegate method: you are calling a method on it.

    If you open the documentation for NSTableView the method is obvious. Try scrolling to tasks and looking at the method names.
     
  5. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #5
    Well Aaron didn't make it that clear in the first place. :confused:

    Cocoa is my first shot at a framework that primarily uses a MVC approach. Having to unlearn bad habits while learning completely new material causes some confusion.

    EDIT: Reread the page... nope. His explanation still doesn't make sense.
     
  6. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #6
    From a procedural to objective transition, the thing to remember about objects is that they are responsive, not proactive. You create your objects and run some setup code in -awakeFromNib or the equivalent and the objects respond to events as they occur (as messages come in). So, basically, a typical Cocoa application will just lie there until you make it do something. The only way things will happen without some action on the part of the user or other input (such as network data coming in) is if you start a NSTimer and connect it to a object/action-method to make things happen on a regular basis. Basically, you are moving from your program polling and doing stuff to a bunch of interlinked objects waiting for stuff to do.
     
  7. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #7
    Don't know if you solved it already, but as I'm using the same book, I thought I'd point out what I did. At the end of your Add method, just put

    Code:
    	[tableView reloadData];
     

Share This Page