numberofSectionInTablesView / NumberofRowsInsection

时间:2012-04-26 11:30:31

标签: iphone ios xcode uitableview

我无法返回numberofSections / numberofRows。我有一本充满数据的字典

-(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;方法 我如何检索索引的部分数量;

和 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;方法,如何检索部分中的行数?

修改

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = [letterArray mutableCopy];
return [[array objectAtIndex:section]count];}

它给了我这个错误: - [__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x8434cc0

提前谢谢

3 个答案:

答案 0 :(得分:1)

for - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView你应该这样做:

return [letterArray count];

for - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分你应该这样做:

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;
}

您应该调用一次并将其保存到类变量中,并将其调用为以下部分:

[countArray count];

对于像这样的部分中的行:

[[countArray objectAtIndex:section] intValue];

答案 1 :(得分:1)

letterArray = [[NSMutableDictionary alloc]init];
indexArray = [[NSMutableArray alloc] init];



[letterArray setValue:[NSMutableArray array] forKey:index];
[indexArray addObject:index];

代表-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return [letterArray count];

代表-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

return [[letterArray objectForKey:[indexArray objectAtIndex:section]] count];

我认为这会对你有所帮助。

答案 2 :(得分:0)

在你的问题中,letterarray是一个NSDictionary。它不会向您显示objectAtIndex选项。尝试将NSMutableDictionary更改为NSMutableArray。然后你会得到选择。

返回(letterarray.count)

NSMutableArray * arr = [[NSMutableArray alloc]init];
for (NSArray *ar in letterarray){
            [arr addObject:ar];
}

返回此arr.count

我正在学习。,。,。