my first tableview

Discussion in 'iOS Development' started by acca2o, Nov 12, 2009.

  1. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    It's my first time

    Please Register or Log in to view images



    I defined a new class TableViewController derived from UIViewController that create and try to fill a UITableView Object.

    Running the application it shows the tableview but empty.
    It seems like the sourceArray is not allocated.
    If i define a new sourceArray in the tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: methods it works fine.

    Where am I wrong?
    This is my TableViewController class:


    TableViewController.h
    [OBJC]#import <UIKit/UIKit.h>


    @interface TableViewController:UIViewController <UITableViewDelegate, UITableViewDataSource>
    {
    NSMutableArray* sourceArray;
    }

    @property(nonatomic, copy) NSMutableArray* sourceArray;

    - (void)loadView;
    - (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView;
    - (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
    - (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger)section;
    - (void)dealloc;
    @end[/OBJC]


    TableViewController.m
    [OBJC]#import "TableViewController.h"


    @implementation TableViewController

    @synthesize sourceArray;

    - (void)loadView
    {
    //crea ed inizializza il root TableView
    UITableView* tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView reloadData];
    self.view = tableView;
    [tableView release];

    //crea l'oggetto dati
    sourceArray = [NSMutableArray arrayWithObjects: @"first", @"second", nil];
    }


    - (NSInteger)numberOfSectionsInTableView: (UITableView*)tableView
    {
    return 1;
    }

    - (NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section
    {
    return [sourceArray count];
    }


    - (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger)section
    {
    return @"Title";
    }


    - (UITableViewCell *)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath
    {
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    cell.textLabel.text = [sourceArray objectAtIndex:indexPath.row];
    return cell;
    }


    -(void) dealloc
    {
    [self.view release];
    [super dealloc];
    }
    @end[/OBJC]
  2. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Please remove it from the quote tags, and use [OBJC<] (Minus the '<'). It's hard to just read code like that.
  3. Shawa

    Shawa Super Moderator

    Joined:
    Jan 31, 2009
    Messages:
    3,188
    Likes Received:
    22
    Device:
    Nexus 4
    BBCode tip, you can use [noparse][NOPARSE][/NOPARSE][/noparse] tags to stop vBull from reading the [noparse][objc][/objc][/noparse] tags.
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Sweet, thanks.

    EDIT: Nevermind, your above post failed that

    Please Register or Log in to view images

    .
  5. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You need not have any of the following methods declared in your interface.

    [OBJC]
    - (void)loadView;
    - (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView;
    - (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
    - (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger)section;
    - (void)dealloc;
    [/OBJC] The middle three are UITableViewDelegate/Datasource method that have already been declared. -[dealloc] and -[loadView] are inherited; when you override an inherited method, you shouldn't redeclare it.

    EDIT: Your table view is empty because in the [loadView] method, you send the tableview object the [reloadData] message before you have created the array holding the data. The tableview object is simply sending those messages (objectAtIndex, etc) to a nil pointer, which won't crash your app, but certainly won't give you the desired results.
  6. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    what a smart reply, what a stupid I am

    Please Register or Log in to view images


    Thanks a lot.

    I moved the line
    sourceArray = [NSMutableArray arrayWithObjects: @"first", @"second";
    at the begin of loadView method.

    But I had a crash.
    Then I changed it in
    sourceArray = [[NSMutableArray arrayWithObjects: @"first", @"second", nil]retain];
    and everything works.

    Is it a need to retain an Array (and release it at the end)?
    Is it the same to use:
    self.sourceArray = [NSMutableArray arrayWithObjects: @"first", @"second";


    About the tag [OBJC<], sorry...I didn't understand
  7. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    You don't even need the [tableView reloadData]; line in there, it will load itself when you create it.

Share This Page