Using delegates for TableView not retrieving data from datasource.

Discussion in 'Mac Programming' started by danchurchill123, Aug 30, 2009.

  1. danchurchill123 macrumors newbie

    Joined:
    Aug 30, 2009
    #1
    hey guys im new to cocoa and objective-c (history with C++ and vb.net though) and i'm going through the aaron hillegass book doing the challenges and i'm doing the one in chapter 5 or 6 where you have to create a delegate with tables.

    here is my project. it doesn't work.


    when you type something into the text field and press the button it is suppose to add the item using delegates to the table. but for the life of me i can't figure out why it's not working. the delegates aren't getting called for some reason. any help ?

    forum wasn't letting me upload the project for some reason so here it is.

    http://uploading.com/files/GGKX8VHH/ToDoList.zip.html
     
  2. scootaru macrumors 6502

    Joined:
    Nov 7, 2008
    Location:
    Columbus, OH
    #2
    A couple things I noticed in your AppController.m file:

    1. In the init method, you have your items array initialize statement commented out, uncomment it.
    2. In your createNewItem method, you need to reload tableView after adding your new item to the items array. The method should look like:

    Code:
    -(IBAction)createNewItem:(id)sender
    {
    NSString *s = [textField stringValue];
    [items addObject:s];
    [tableView reload];  //guaranteed not to work without this
    }
    3. You need to release your array in a dealloc method as such:

    Code:
    -(void)dealloc {
        [items release];
        [super dealloc];
    }
    
    I'm not on my mac right now, so I can't actually build it and I'm assuming everything is linked properly in Interface Builder.
     
  3. danchurchill123 thread starter macrumors newbie

    Joined:
    Aug 30, 2009
    #3
    thanks alot! that worked.

    although the method reload wasn't recognized. had to use reloadData instead.

    and with the dealloc method you provided do i need to call that from somewhere or is it done automatically by the program at termination ? i'm still very new to cocoa and objective-c programming so i'm trying to find my feet hehe.

    oh and this was tripping up the program too

    Code:
     [items setDelegate:self]; 
     
  4. scootaru macrumors 6502

    Joined:
    Nov 7, 2008
    Location:
    Columbus, OH
    #4
    reloadData is the message I meant to send to tableView. I am new to this kind of programming as well, sorry about that. As far as dealloc, you are correct, it is called before program termination to clean things up.
     

Share This Page