Sheehan Alam
Sheehan Alam

Reputation: 60919

Memory problem with basic UITableView when scrolling

I have a very simple UITableView that has 3 sections, and 3 rows per section.

#pragma mark -
#pragma mark UITableView delegate methods

- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    return cell;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView 
{ 
    if (tblView == self.tableView) {
        return 3;
    }
    else {
        return 1; 
    }
}

Everything shows up fine, but as soon as I scroll my application crashes and my debugger tells me:

***** -[ProfileViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x5ae61b0**

I'm not exactly sure what I am doing wrong.

EDIT: This is how I am displaying the ProfileViewController:

ProfileViewController* profileView = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
    profileView.user_name = username;
    profileView.message_source = messageSource;
    [self.navigationController pushViewController:profileView animated:YES];
    [profileView release];

Upvotes: 2

Views: 1108

Answers (4)

Instead of using

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

please use

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

And change your implementation code as follow:

Don't use the below code in "cellForRowAtIndexPath" method. Instead use in "didSelectRowAtIndex" method.

In header(.h) file:

ProfileViewController* profileView;

In implementation (.m) file:

if(profileView==nil) profileView = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil]; profileView.user_name = username; profileView.message_source = messageSource; [self.navigationController pushViewController:profileView animated:YES];

Upvotes: 0

matt
matt

Reputation: 21

something that helped me a similar problem was realizing the following: you may need to retain a table view controller by setting it up as an IBOutlet if it is a subview of another view - i.e. the parent must, in some way, be retaining the child.

Upvotes: 0

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

Looks like your ProfileViewController instance is getting deallocated somehow. Make sure you're not calling its -autorelease after creating it.

Upvotes: 2

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

Your code seems right. Your bug might be in your model or in the cell configuration. Turn on zombie support for search this kind of error.

Upvotes: 1

Related Questions