问题描述
我在返回numberofSections/numberofRows时遇到麻烦.我有一本充满数据的字典
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);}
和它的NSLogs
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";
}
);
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;方法如何检索索引的节数;
for the -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; methodhow do i retrieve the number of sections for index;
以及-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分;方法,我该如何获取该部分中的行数?
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];}
它给了我这个错误:-[__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x8434cc0
and it gives me this error: -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8434cc0
提前谢谢
推荐答案
对于-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView,您应该执行以下操作:
for -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you should do something like this:
return [letterArray count];
对于-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分,您应该执行以下操作:
for -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section you should do something like this:
NSArray *array = letterArray;
return [[array objectAtIndex:section] count];
编辑试试这个:
-(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];
这篇关于numberofSectionInTablesView/NumberofRowsInsection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!