Resolved ipad UITableView array -> crash when drog down the tableView

Discussion in 'iOS Programming' started by kimti, Feb 22, 2011.

  1. kimti, Feb 22, 2011
    Last edited: Mar 14, 2011

    kimti macrumors newbie

    Joined:
    Feb 22, 2011
    #1
    IOS 4.2

    create project and select "Split View-based Application"

    define the array in RootViewController.h file:

    Code:
    NSMutableArray *a;
    
    init the array in viewDidLoad method

    Code:
    a = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
    

    implementation method:
    Code:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;
    }
    if(indexPath.row < [a count]){
    cell.textLabel.text = [a objectAtIndex:indexPath.row];
    }
    return cell;
    }
    
    ---------------------------------------
    Solution: insert follow code after init the array a:
    Code:
    [a retain];
    
    thx admanimal.
     
  2. mheidt macrumors member

    Joined:
    Jun 7, 2005
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    Did you retain a? The code as posted results in a potentially being invalid as soon as viewDidLoad returns.
     
  4. kimti, Feb 22, 2011
    Last edited by a moderator: Feb 22, 2011

    kimti thread starter macrumors newbie

    Joined:
    Feb 22, 2011
    #4
    thx ,but i have follow:


    Code:
    if(indexPath.row < [a count]){
          cell.textLabel.text = [a objectAtIndex:indexPath.row];
    }
    i not retain a ,i noly follow:

    Code:
    a = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
     
  5. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    You should probably read up on memory management then. But for now, add
    Code:
    [a retain];
    after you call arrayWithObjects and see what happens.
     
  6. kimti thread starter macrumors newbie

    Joined:
    Feb 22, 2011
    #6
    oh yes man,you are right.
    much thank you .
     
  7. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #7
    You probably did it the correct way in your own code if it is working, but the solution you added to the original post is incorrect. The retain message should go after you initialize the array.
     

Share This Page