ada*_*ale 5 objective-c uitableview ios
我在使用带有节标题的索引表时遇到问题.目前我在右侧有索引,我有正确显示的部分标题,标题只显示该部分内是否有数据.
我遇到的问题是将NSArray分成几部分,这样我就可以正确计算numberOfRowsInSections.目前我有正确数量的部分显示正确的标题,但所有数据都在每个部分,而不是根据名称的第一个字母拆分.
以下是目前的截图:
所有数据都分为每个部分,每行5行.部分(3)的数量是正确的
我的代码如下:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [firstLetterArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableSet *mySet = [[NSMutableSet alloc] init];
BRConnection *connection = nil;
NSMutableArray *firstNames = [[NSMutableArray alloc] init];
for (connection in _connections)
{
[firstNames addObject:connection.firstName];
}
firstNamesArray = firstNames;
NSLog(@"%@", firstNamesArray);
for ( NSString *s in firstNames)
{
if ([s length] > 0)
[mySet addObject:[s substringToIndex:1]];
}
NSArray *indexArray = [[mySet allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
firstLetterArray = indexArray;
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if ([title isEqualToString:@"{search}"])
{
[tableView setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)];
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ConnectionCell"];
// Display connection in the table cell
BRConnection *connection = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
connection = [searchResults objectAtIndex:indexPath.row];
} else {
connection = [_connections objectAtIndex:indexPath.row];
}
cell.textLabel.text = connection.fullName;
cell.textLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:18];
cell.detailTextLabel.text = connection.company;
cell.detailTextLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:12];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
NSUInteger sections = [firstLetterArray count];
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [_connections count];
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,我似乎无法将NSArray conenctions分成一个按字母顺序排列的列表,以获得一个部分中的正确行.在此先感谢大家!
您在哪里以及如何填充_connections?您正在使用该数组来确定每个部分的行数并填充这些行,但是_connections返回整个列表.您需要_connections按字母顺序拆分数据.
比如,也许你可以使用NSMutableArray的NSMutableArray由字母s组数据.由于您似乎已经知道如何按字母顺序排序,现在您只需要识别每个字符串的第一个字符即可正确分组.为此,请尝试:
NSString *currentPrefix;
// Store sortedConnections as a class variable (as you've done with _connections)
// so you can access it to populate your table
sortedConnections = [[NSMutableArray alloc] init];
// Go through each connection (already ordered alphabetically)
for (BRConnection *connection in _connections) {
// Find the first letter of the current connection
NSString *firstLetter = [connection.fullName substringToIndex:1];
// If the last connection's prefix (stored in currentPrefix) is equal
// to the current first letter, just add the connection to the final
// array already in sortedConnections
if ([currentPrefix isEqualToString:firstLetter]) {
[[sortedConnected lastObject] addObject:connection];
}
// Else create a new array in sortedConnections to contain connections starting
// with this current connection's letter.
else {
NSMutableArray *newArray = [[NSMutableArray alloc] initWithObject:connection];
[sortedConnections addObject:newArray];
}
// To mark this latest array's prefix, set currentPrefix to contain firstLetter
currentPrefix = firstLetter;
}
Run Code Online (Sandbox Code Playgroud)
(即使第一个字母未知,这种方式也会有效.)
然后要获取每个部分的行数,请使用[sortedConnections objectAtIndex:section]而不是_connections:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [[sortedSearchResults objectAtIndex:section] count]; // hypothetically
} else {
return [[sortedConnections objectAtIndex:section] count];
}
}
Run Code Online (Sandbox Code Playgroud)
并填充表基本上使用相同的方法[sortedConnections objectAtIndex:indexPath.section]:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ConnectionCell"];
// Display connection in the table cell
BRConnection *connection = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
connection = [[sortedSearchResults objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; // hypothetically
} else {
connection = [[sortedConnections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
cell.textLabel.text = connection.fullName;
cell.textLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:18];
cell.detailTextLabel.text = connection.company;
cell.detailTextLabel.font = [UIFont fontWithName:@"TitilliumText25L-400wt" size:12];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助你,我不知道是否是最好的方法,但它的工作原理=)
NSArray *names = @[@"Ana Carolina", @"Ana carolina", @"Ana luiza", @"leonardo", @"fernanda", @"Leonardo Cavalcante"];
NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0];
for( NSString*string in names ){
[firstCharacters addObject:[[string substringToIndex:1] uppercaseString]];
}
NSArray *allLetters = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
int indexLetter = 0;
NSMutableArray *separeNamesByLetters = [NSMutableArray new];
for (NSString *letter in allLetters) {
NSMutableDictionary*userBegeinsWith = [NSMutableDictionary new];
[userBegeinsWith setObject:letter forKey:@"letter"];
NSMutableArray *groupNameByLetters = [NSMutableArray new];
NSString *compareLetter1 = [NSString stringWithFormat:@"%@", allLetters[indexLetter]];
for (NSString*friendName in names) {
NSString *compareLetter2 = [[friendName substringToIndex:1] uppercaseString];
if ( [compareLetter1 isEqualToString:compareLetter2] ) {
[groupNameByLetters addObject:friendName];
}
}
indexLetter++;
[userBegeinsWith setObject:groupNameByLetters forKey:@"list"];
[separeNamesByLetters addObject: userBegeinsWith];
}
NSLog(@"%@", separeNamesByLetters);
Run Code Online (Sandbox Code Playgroud)
输出:
(
{
letter = A;
list = (
"ana carolina",
"Ana carolina",
"Ana luiza"
);
},
{
letter = F;
list = (
fernanda
);
},
{
letter = L;
list = (
leonardo,
"Leonardo Cavalcante"
)
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4021 次 |
| 最近记录: |