Sectioned tableview problem
I have a database app i am making and i can extract the results from a sqlite database and put them in the table, however i need to mak开发者_开发问答e them sort alphabetically in sections.
So now i have this code
AArray = [[NSMutableArray alloc] init];
BArray = [[NSMutableArray alloc] init];
CArray = [[NSMutableArray alloc] init];
DArray = [[NSMutableArray alloc] init];
EArray = [[NSMutableArray alloc] init];
FArray = [[NSMutableArray alloc] init];
GArray = [[NSMutableArray alloc] init];
HArray = [[NSMutableArray alloc] init];
IArray = [[NSMutableArray alloc] init];
JArray = [[NSMutableArray alloc] init];
KArray = [[NSMutableArray alloc] init];
LArray = [[NSMutableArray alloc] init];
MArray = [[NSMutableArray alloc] init];
NArray = [[NSMutableArray alloc] init];
OArray = [[NSMutableArray alloc] init];
PArray = [[NSMutableArray alloc] init];
QArray = [[NSMutableArray alloc] init];
RArray = [[NSMutableArray alloc] init];
SArray = [[NSMutableArray alloc] init];
TArray = [[NSMutableArray alloc] init];
UArray = [[NSMutableArray alloc] init];
VArray = [[NSMutableArray alloc] init];
WArray = [[NSMutableArray alloc] init];
XArray = [[NSMutableArray alloc] init];
YArray = [[NSMutableArray alloc] init];
ZArray = [[NSMutableArray alloc] init];
and i have code to move each item from the database into the relevant array, this all works fine.
I then have this code:
All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,@"A",BArray,@"B",CArray,@"C",DArray,@"D",EArray,@"E",FArray,@"F",GArray,@"G",HArray,@"H",IArray,@"I",JArray,@"J",KArray,@"K",LArray,@"L",MArray,@"M",NArray,@"N",OArray,@"O",PArray,@"P",QArray,@"Q",RArray,@"R",SArray,@"S",TArray,@"T",UArray,@"U",VArray,@"V",WArray,@"W",XArray,@"X",YArray,@"Y",ZArray,@"Z",nil];
AllArray = [NSArray alloc];
AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:@selector(compare:)]];
and this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [AllArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
int sectionindex = section;
return [AllArray objectAtIndex:sectionindex];
}
When i run it works fine, however if i scroll up and down a few times the app crashes without any error message. It is something to do with the sections as it crashes when i add them in, but i just cannot see what im doing wrong
Everythings declared in the h file and i @property and @synthesize the following
@property (nonatomic,retain) NSMutableDictionary *All;
@property (nonatomic,retain) NSArray *AllArray;
@property (nonatomic, retain) UITableView *TableView;
If anyone could help me it would be really great!
Thanks!
Your dictionary "All" does not seem to be retained. You appear to assign the dictionary object directly to the instance variable, not to the property (in which case you would have used self.All).
If your app crashes without a message, then make sure to enable the Breakpoints button in Xcode's toolbar. This will run the app in the debugger, which will give you more helpful information about the crash. Setting NSZombieEnabled to YES also helps.
Take a look here:
- Documentation
The simplest approach is, to provide a sort selector (see the link for details):
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
精彩评论