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

imfrog2002

macrumors 6502
Original poster
Oct 20, 2007
316
0
Okay, I didn't see anything on search, so sorry if this was posted before.

I'm trying to make a iPhone app, and I can't figure out how to get a second cell/row. I'm using the Navigation Base App template, and in the RootViewController.m file, I have the "numberOfSectionsInTableView" set to 1, and the "numberOfRowsInSection" set to 2. I can't get it to change the cell text on the second row/cell. What code in there do I need to copy/edit. I'm still new at this, so I'm mostly looking up stuff as I go. This is the only thing that I can't find online. Thanks in advance!
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
EDIT: Man, I've been looking at this code too long. Can a mod move this to the iPhone programming forum? Thanks. *bangs head on desk*

Okay, I didn't see anything on search, so sorry if this was posted before.

I'm trying to make a iPhone app, and I can't figure out how to get a second cell/row. I'm using the Navigation Base App template, and in the RootViewController.m file, I have the "numberOfSectionsInTableView" set to 1, and the "numberOfRowsInSection" set to 2. I can't get it to change the cell text on the second row/cell. What code in there do I need to copy/edit. I'm still new at this, so I'm mostly looking up stuff as I go. This is the only thing that I can't find online. Thanks in advance!

Do you also have cellForRowAtIndexPath implemented?
 

imfrog2002

macrumors 6502
Original poster
Oct 20, 2007
316
0
HTML:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}

	[cell setText = @"Text 1"];
	// Set up the cell
	return cell;
}

That's what it is before edits.

I was thinking that adding another cell setText line is all I needed to do (and change the numberOfRowsInSection to 2,) but I've found that's not the case.
 

imfrog2002

macrumors 6502
Original poster
Oct 20, 2007
316
0
Thanks to a very kind person, I was able to fix it! For future reference, and anybody that searches, this is what needed to happen:

HTML:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	switch (indexPath.row) {
		case 0:
			cell.text = @"Database";
			break;
		case 1:
			cell.text = @"Settings";
			break;
	}

	
	// Set up the cell
	return cell;
}

Thanks again! :D
 

Fremen93

macrumors newbie
Aug 25, 2008
3
0
Thanks for posting the "fix"!
That is so helpful for those of searching for similar issues.

By the way, one thing I don't quite understand is why the identifier is always a hard coded string? Shouldn't the identifier be unique for each cell? otherwise how does it know which cell to retrieve from cache? I must be missing something obvious...
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Thanks for posting the "fix"!
That is so helpful for those of searching for similar issues.

By the way, one thing I don't quite understand is why the identifier is always a hard coded string? Shouldn't the identifier be unique for each cell? otherwise how does it know which cell to retrieve from cache? I must be missing something obvious...

It should be unique for each -type- of cell. As long as all of your cells are the same basic type and are just populated with different data, they can all have the same ID.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.