gpt4 book ai didi

iOS TableView : Grouping rows into sections

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:46 26 4
gpt4 key购买 nike

我在表格 View 中显示一个动态的名字列表,我试图根据名字的第一个字母将它们分成几个部分...

我创建了一个数组,其中包含按字母顺序排列的字母列表

charIndex = [[NSMutableArray alloc] init];

for(int i=0; i<[appDelegate.children count]-1; i++)
{
// get the person
Child *aChild = [appDelegate.children objectAtIndex:i];

// get the first letter of the first name
NSString *firstLetter = [aChild.firstName substringToIndex:1];

NSLog(@"first letter: %@", firstLetter);

// if the index doesn't contain the letter
if(![charIndex containsObject:firstLetter])
{
// then add it to the index
NSLog(@"adding: %@", firstLetter);
[charIndex addObject:firstLetter];
}
}

我已经设置了部分的数量和标题

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// set the number of sections in the table to match the number of first letters
return [charIndex count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// set the section title to the matching letter
return [charIndex objectAtIndex:section];
}

但是我不知道应该放什么

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

}

最佳答案

您可以添加一个字典来跟踪首字母相同的人数。快速未经测试的代码:

charIndex = [[NSMutableArray alloc] init];
charCount = [[NSMutableDictionary alloc] init];

for(int i=0; i<[appDelegate.children count]-1; i++)
{
// get the person
Child *aChild = [appDelegate.children objectAtIndex:i];

// get the first letter of the first name
NSString *firstLetter = [aChild.firstName substringToIndex:1];

NSLog(@"first letter: %@", firstLetter);

// if the index doesn't contain the letter
if(![charIndex containsObject:firstLetter])
{
// then add it to the index
NSLog(@"adding: %@", firstLetter);
[charIndex addObject:firstLetter];
[charCount setObject:[NSNumber numberWithInt:1] forKey:firstLetter];
}
[charCount setObject:[NSNumber numberWithInt:[[charCount objectForKey:firstLetter] intValue] + 1] forKey:firstLetter];
}

然后在:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[charCount objectForKey:[charIndex objectAtIndex:section]];
}

关于iOS TableView : Grouping rows into sections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com