Hi guys,
actually I'm have a very simple problem (that's for sure) I just can't find the root cause.
The problem is, that after calling the numberOfSectionsInTableView Method to get the number of Sections (24 in my case) the method numberOfRowsInSection gets called but with the last section (23).
This causes an out of bound index when scrolling.
So, what happens? I'm receiving the data of the last section (23) and even the number of rows of the last section (23) and afterwards the sections are moving forward like 0,1,2,3,etc...
An example: I have a section with the section header "#" for special characters. In my case the last section is the "X" for all values beginning with an "X".
When I call my App, the section title has been set correctly to "#" but the data shows all the "X" values. At this point I have 31 "X" values. When I now scroll to the last of the 31 rows, the app crashes because of an out of index error.
I think everything would work correctly If the sections wouldn't start with the last one and afterwards continue with the first ones (23,0,1,2,3,4,etc). I just can't find the place where or why the section hast the value 23 on the first place.
---EDIT 2 BEGINS: initialising the array:
alphabetArray = [[NSMutableArray alloc] init];
First I fill a dictionary like this (for all letters incl. special chars):
charDict = [[NSMutableDictionary alloc] init];
if ([charSpecialArray count] > 0) {
[charDict setObject:charSpecialArray forKey:@"Special"];
[alphabetArray addObject:@"Special"];
}
if ([charAArray count] > 0) {
[charDict setObject:charAArray forKey:@"A"];
[alphabetArray addObject:@"A"];
}
---EDIT 2 ENDS
So here I'm just filling the first dictionary Object with all "#" values (going on with all the other values)
Then I call the numberOfSectionsInTableView method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [charDict count];
}
here the numberOfRowsInSection goes:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *key = [alphabetArray objectAtIndex:section];
NSArray *array = [charDict objectForKey:key];
return [array count];
}
}
and the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
if ([charSpecialArray count] > 0) {
Char *charSpecial = (Char *)[[charSpecialArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 1;
cell.detailTextLabel.numberOfLines = 2;
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];
cell.textLabel.text = charSpecial.ta;
cell.detailTextLabel.text = charSpecial.descriptionEN;
}
if ([charAArray count] > 0) {
Char *charA = (Char *)[[charAArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 1;
cell.detailTextLabel.numberOfLines = 2;
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];
cell.textLabel.text = charA.ta;
cell.detailTextLabel.text = charA.descriptionEN;
}
again for all characters...
Any suggestions how to fix that?
added initialization code .h:
#import <UIKit/UIKit.h>
@interface Char : NSObject {
NSString *ta;
NSString *report;
NSString *descriptionDE;
NSString *descriptionEN;
}
@property (nonatomic, retain) NSString *ta;
@property (nonatomic, retain) NSString *report;
@property (nonatomic, retain) NSString *descriptionDE;
@property (nonatomic, retain) NSString *descriptionEN;
-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde descriptionEN:(NSString *)den;
@end
initialization code .m:
#import "SCode.h"
@implementation Char
@synthesize ta, report, descriptionDE, descriptionEN;
-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde descriptionEN:(NSString *)den {
self.ta = t;
self.report = re;
self.descriptionDE = dde;
self.descriptionEN = den;
return self;
}
- (void)dealloc {
[ta release];
[report release];
[descriptionDE release];
[descriptionEN release];
[super dealloc];
}
@end