gpt4 book ai didi

ios - 如何为标题索引uitableview iOS的对象组成数组?

转载 作者:行者123 更新时间:2023-12-01 18:42:03 25 4
gpt4 key购买 nike

我正在制作标题索引的事件邀请列表。我有一个带有EventStatus对象的数组。我正在为表制作_data数组,例如

for (int i = 0; i < eventStatusList.count; i++) {
NSString *firstName = ((OCEventStatus*)eventStatusList[i]).user.firstName;
[_sortedUsers setObject:((OCEventStatus*)eventStatusList[i]).user.firstName forKey:[firstName substringToIndex:1]];
}
_sortedUserTitles = [[[_sortedUsers allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] mutableCopy];

_data = [eventStatusList mutableCopy];
[self.dataTableView reloadData];

而且我认为这个for循环的东西太慢了。有没有办法很好地做到这一点?以下是使用 UITablrViewDataSource方法组成逻辑的标题索引。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_sortedUserTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [_sortedUserTitles objectAtIndex:section];
NSArray *sectionUsers = [_sortedUsers objectForKey:sectionTitle];
return [sectionUsers count];

}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// return animalSectionTitles;
return _alphabetArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [_sortedUserTitles indexOfObject:title];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_sortedUserTitles objectAtIndex:section];
}

这是错误的,因为在 _sortedUsers字典中有一个字符串而不是一个数组。我该如何解决?同时,请提出一种快速,良好的方式来实现该标题索引。

最佳答案

如果要创建名字列表,请尝试此操作。

在界面上:

@property(nonatomic, strong)NSMutableDictionary *dict;
@property(nonatomic, strong)NSMutableArray *alphabet;
_dict = [NSMutableDictionary new];
_alphabet = [NSMutableArray new];
[eventStatusList sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
OCEventStatus *ev1 = (OCEventStatus*)obj1;
OCEventStatus *ev2 = (OCEventStatus*)obj2;
return [ev1.firstName compare:ev2.firstName];
}];

for(OCEventStatus *state in eventStatusList){
NSString *firstchar = [[state.firstName substringToIndex:1] lowercaseString];
if([dict objectForKey:firstchar]==nil){
NSMutableArray *tmp = [NSMutableArray new];
[tmp addObject:state];
[_dict setObject:tmp forKey:firstchar];
[_alphabet addObject:firstChar];
}else{
[[dict objectForKey:firstchar] addObject:state];
}
}

现在,您在字典中有了一个名字数组,其中以第一个字母为键,例如:a-> [“Alfred”,“Albert”,...]

在数据源方法中,您必须像这样返回它...
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return dict.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection;(NSInteger)section
{
return [dict objectForKey:[alphabet objectAtIndex:section]].count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
OVEventStatus *state = [[dict objectForKey:[alphabet objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = state.firstName;
return cell;

}

请尝试是否适合您

更新:

如果您还想对数组进行排序,我建议您首先对eventListBy firstName进行排序!因此,当您遍历eventStatusList时,您将拥有正确的顺序。

关于ios - 如何为标题索引uitableview iOS的对象组成数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465828/

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