Opening incorrect nib file with corresponding object in Table View
Hey guys, I'm implementing a Table View into my project. First I populated my table, then I manage to successfully create 4 categories and give a number of rows in each of those categories, but here's my problem. The first category (which has 7 objects) is working fine when each data in that category opens their own nib files. But on the second category which has 3 objects, the 3 objects are actually opening nib files from the first category, which is not what I want. I created and assigned each objects in my Table View their own nib files to open, but like I said the objects from the second and also the third and fourth categories are opening the nib files from the first category. Here's my code how I separate the objects in my Table View:
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
// Category
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (section == 0) return @"Category 1";
if (section == 1) return @"Category 2";
if (section == 2) return @"Category 3";
if (section == 3) return @"Category 4";
return @"Other";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (section == 0) return 7;
if (section == 1) return 3;
if (section == 2) return 6;
if (section == 3) return 5;
return 0;
}
// Customi开发者_Python百科ze the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
if ( indexPath.section == 1 ) row += 7;
if ( indexPath.section == 2 ) row += 10;
if ( indexPath.section == 3 ) row += 16;
if ( indexPath.section == 4 ) row += 21;
cell.textLabel.text = [glossaryArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
and to open each cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[glossaryArray objectAtIndex:indexPath.row] isEqual:@"First View Controller"]) {
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.navigationController firstVC animated:YES];
[firstVC release];
}
else if ([[glossaryArray objectAtIndex:indexPath.row] isEqual:@"Second View Controller"]) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
else if (...
So that's pretty much how my nib files are opening each view, but like again, the problem is that they are not opening their own views, they are opening the nib files from the first category, which is the incorrect nib files. Sop I hope someone can help me figure out this problem, thanks
I believe your problem is that you are trying to map a 2 dimensional array onto a 1 dimensional array.
In your didSelectRowAtIndexPath: you are using indexPath.row, without checking to see what indexPath.section you are in. The row is 0 based on the section, not absolute to the number of rows in your table view. So you end up with something like
Section 0 Row 0 Row 1
Section 1 Row 0 Row 1
Here is the documentation on the NSIndexPath UIKit additions:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSIndexPath_UIKitAdditions/Reference/Reference.html#//apple_ref/doc/uid/TP40007175
It looks like your glossaryArray attempts to fix this by using an absolute index, so Section 1, Row 1 becomes index 3 into glossaryArray. What is happening though is indexPath.row is returning 1, instead of your expected 3.
You'll have to map the 2 dimensional indexPath onto one dimension or update your glossaryArray to be 2 dimensional.
精彩评论