UITableView reloadData problem

Discussion in 'iOS Programming' started by twain249, Oct 19, 2011.

  1. twain249, Oct 19, 2011
    Last edited: Oct 19, 2011

    twain249 macrumors newbie

    Joined:
    Oct 19, 2011
    #1
    This is my first iPhone app and first time I'm trying to use a UITableView so please be explicit. What I have is a Tab Navigated Window where on of the views is a list with a title and a series of buttons (add, edit, and remove) at the bottom and a UITableView as the rest of the nib. The TableView's outlet is in my controller and my controller also acts as the delegate and the datasource. Right now I'm circumventing the actual add (which opens a new view allowing the user to fill out a form) and simply adding a new value to my listData NSMutableArray. Now after I add the new Object I call [myTableView reloadData] and the app simply closes without any crash report.

    Here is my code

    Code:
    - (void)viewDidLoad {
    	
    	UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style: UITableViewStylePlain];
    	
    	[tableView setDelegate: self];
    	[tableView setDataSource: self];
    	myTableView = tableView;
    	[tableView release];
    	
    	self.listData = [[NSMutableArray alloc] initWithObjects: @"Test", @"Hello", @"World", nil];
    	
        [super viewDidLoad];
    }
    
    Code:
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    }
    
    Code:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	NSLog(@"Rows in Section Reached");
    	return [self.listData count];
    }
    
    Code:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	static NSString *MyIdentifier = @"MyIdentifier";
    
    	NSLog(@"Table view reached");
    	
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if(cell == nil) {
    		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    	}
    	
    	NSLog(cell.description);
    	
    	cell.textLabel.text = [self.listData objectAtIndex: indexPath.row];
    
    	return cell;
    }
    
    Code:
    -(IBAction)removeFilter: (id) sender {
    	NSLog(@"Remove Filter Clicked\n");
    	[self add: @"GoodBye"];
    }
    
    Code:
    -(void)add:(NSObject *)obj {
    	NSString *string = [NSString stringWithFormat:@"Number of Items: %d", 
    						[self.listData count]];
    	NSLog(string);
    	[self.listData addObject: obj];
    	string = [NSString stringWithFormat:@"Number of Items: %d", 
    						[self.listData count]];
    	NSLog(string);
    	
    	[self.myTableView reloadData];
    }
    
    If I remove the [tableView release] from the viewDidLoad method the app no longer exits but nothing happens (the log shows the number of items in listData is increasing however).

    Any help would be greatly appreciated.
     
  2. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #2
    I suggest you read the memory management guide to fully understand what your code does, but for now, try the following things:

    1) Your code, as is, creates a table view object, assigns self to it's delegate and datasource properties, assigns that object to a ivar or local variable, then releases it. The problem is that a) nothing retains that object, so it gets released when you release it, and b) it's never added to a view, so there is no way to see it. Short recommendation: delete the code that deals with your locally-created tableview (including the line that says release).

    2) You state your nib already has a tableview. That tableview (presuming your class is a subclass of UITableViewController) is connected to the self.tableView property, so you don't need to create a new one. Set the delegate and dataSource properties of self.tableView to self. Now you're using the tableView that is part of the view.

    Your new code should look very similar to what you had:
    Code:
    - (void)viewDidLoad {
    	self.tableView.delegate = self;
    	self.tableView.dataSource = self;
    	
    	self.listData = [[NSMutableArray alloc] initWithObjects: @"Test", @"Hello", @"World", nil];
    	
        [super viewDidLoad];
    }
    
    3) Finally, in your add: method, change it to use self.tableView instead of self.myTableView.
     
  3. twain249 thread starter macrumors newbie

    Joined:
    Oct 19, 2011
    #3
    Thanks I added got rid of the locale definition and everything works now
     

Share This Page