Creating Another Row Problem

Discussion in 'iOS Programming' started by imfrog2002, Aug 6, 2008.

  1. imfrog2002 macrumors 6502

    Joined:
    Oct 20, 2007
    #1
    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!
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  3. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #3
    Do you also have cellForRowAtIndexPath implemented?
     
  4. imfrog2002 thread starter macrumors 6502

    Joined:
    Oct 20, 2007
    #4
    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.
     
  5. imfrog2002 thread starter macrumors 6502

    Joined:
    Oct 20, 2007
    #5
    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
     
  6. Fremen93 macrumors newbie

    Joined:
    Aug 25, 2008
    #6
    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...
     
  7. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #7
    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.
     

Share This Page