Reputation: 155
I am having trouble returning the numberofSections/numberofRows. I have a dictionary full of data
-(void)setUpContacts{
//set up the contacts
NSString *string = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letterArray = [[NSMutableDictionary alloc]init];
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
firstLetter = [info objectForKey:@"lastName"];
index = @"_";
if([firstLetter length] > 0){
firstLetter =[firstLetter substringToIndex:1];
firstLetter= [firstLetter capitalizedString];
NSRange range = [string rangeOfString:firstLetter];
if(range.length >= 0){
index= firstLetter;
}
}
if(![letterArray objectForKey:index]){
[letterArray setValue:[NSMutableArray array] forKey:index];
}
[[letterArray objectForKey:index] addObject:info];
}
NSLog(@"%@",letterArray);}
and it NSLogs with this
C = (
{
firstName = Alex;
kind = Mobile;
lastName = Chang;
name = "Alex Chang (Mobile)";
number = "(555) 555-5555";
},
{
firstName = YuYu;
kind = Mobile;
lastName = Chen;
name = "YuYu Chen (Mobile)";
number = "(408) 112-2334";
},
{
firstName = Chris;
kind = Mobile;
lastName = Choi;
name = "Chris Choi (Mobile)";
number = "(999) 999-9999";
},
{
firstName = Kevin;
kind = Mobile;
lastName = Chung;
name = "Kevin Chung (Mobile)";
number = "1 (231) 241-2312";
}
);
H = (
{
firstName = Danny;
kind = Mobile;
lastName = Huang;
name = "Danny Huang (Mobile)";
number = "(408) 599-9770";
},
{
firstName = Ice;
kind = Mobile;
lastName = Huang;
name = "Ice Huang (Mobile)";
number = "(408) 444-4444";
}
);
K = (
{
firstName = Will;
kind = Mobile;
lastName = King;
name = "Will King (Mobile)";
number = "(415) 123-4567";
}
);
L = (
{
firstName = "";
kind = iPhone;
lastName = LastName;
name = " LastName (iPhone)";
number = "(408) 123-2555";
},
{
firstName = david;
kind = Mobile;
lastName = lub;
name = "david lub (Mobile)";
number = "(666) 666-1111";
}
);
for the -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; method how do i retrieve the number of sections for index;
and for the -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; method, how do i retreive the number of rows in the section?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = [letterArray mutableCopy];
return [[array objectAtIndex:section]count];}
and it gives me this error: -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8434cc0
Thank you in advance
Upvotes: 0
Views: 851
Reputation: 10303
for -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you should do something like this:
return [letterArray count];
for -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section you should do something like this:
NSArray *array = letterArray;
return [[array objectAtIndex:section] count];
EDIT Try this:
-(NSArray *)getLetterArrayCount {
NSMutableArray *countArray = [[NSMutableArray alloc] init];
NSNumber *subCount;
for (id key_main in letterArray) {
subCount = 0;
for (id key_sub in [letterArray objectForKey:key_main]) {
subCount = [NSNumber numberWithInt:[subCount intValue] + 1];
}
[countArray addObject:subCount];
}
return countArray;
}
You should call it once and save it to a class variable and call it like this for sections:
[countArray count];
And for the rows in a section like this:
[[countArray objectAtIndex:section] intValue];
Upvotes: 1
Reputation: 6718
letterArray = [[NSMutableDictionary alloc]init];
indexArray = [[NSMutableArray alloc] init];
[letterArray setValue:[NSMutableArray array] forKey:index];
[indexArray addObject:index];
for -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
:
return [letterArray count];
for -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
:
return [[letterArray objectForKey:[indexArray objectAtIndex:section]] count];
I think it will be helpful to you.
Upvotes: 1
Reputation: 321
in ur question the letterarray is an NSDictionary. it wont show You the objectAtIndex option. try to change the NSMutableDictionary into NSMutableArray. then you will get the option.
and
return(letterarray.count)
NSMutableArray * arr = [[NSMutableArray alloc]init];
for (NSArray *ar in letterarray){
[arr addObject:ar];
}
Return this arr.count
i am Learning.,.,.
Upvotes: 0