Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kimti

macrumors newbie
Original poster
Feb 22, 2011
10
0
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.
 
Last edited:
Did you retain a? The code as posted results in a potentially being invalid as soon as viewDidLoad returns.
 
try a -1 in the objectAtIndex of "a"

thx ,but i have follow:


Code:
if(indexPath.row < [a count]){
      cell.textLabel.text = [a objectAtIndex:indexPath.row];
}

Did you retain a? The code as posted results in a potentially being invalid as soon as viewDidLoad returns.

i not retain a ,i noly follow:

Code:
a = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
 
Last edited by a moderator:
You should probably read up on memory management then. But for now, add
Code:
[a retain];
after you call arrayWithObjects and see what happens.
 
You should probably read up on memory management then. But for now, add
Code:
[a retain];
after you call arrayWithObjects and see what happens.

oh yes man,you are right.
much thank you .
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.