iphone - 表格 View 中的节数/行节数

标签 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:]: unrecognized selector sent to instance 0x8434cc0

提前致谢

最佳答案

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

return [letterArray count];

对于 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 你应该这样做:

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

关于iphone - 表格 View 中的节数/行节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10332507/

相关文章:

ios - 为什么我的图像无法写入文件和显示?

ios - 如何在 Xcode 7 中将四个按钮对齐死点?

iOS App > 无法设置远程报价 sdp : Called with SDP without DTLS fingerprint

ios - 配置文件的到期日期是否取决于链接的证书日期?

ios - 我可以从实现文件创建 .xib 吗?

arrays - 如何在我的 UI 测试中从 XCUIApplication 访问 NSApplicationDelegate

iphone - 从在线 Plist 文件中获取字符串?

iphone - 显示应用程序在后台运行的持续时间

iphone - 如何查看给定 iOS ipa 文件中定义的 NSString

javascript - 我在 IOS 中遇到了 UIWebview 对象的奇怪问题