Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用标题索引uitableview iOS的对象组成数组?_Ios_Objective C_Arrays_Uitableview - Fatal编程技术网

如何使用标题索引uitableview iOS的对象组成数组?

如何使用标题索引uitableview iOS的对象组成数组?,ios,objective-c,arrays,uitableview,Ios,Objective C,Arrays,Uitableview,我正在制作一个标题索引的活动邀请列表。我有一个带有EventStatus对象的数组。我正在为table-like制作\u data数组 for (int i = 0; i < eventStatusList.count; i++) { NSString *firstName = ((OCEventStatus*)eventStatusList[i]).user.firstName; [_sortedUsers setObject:((OCEventStatus*)event

我正在制作一个标题索引的活动邀请列表。我有一个带有
EventStatus
对象的数组。我正在为table-like制作
\u 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];
}


这是一个错误,因为在
\u sortedUsers
字典中有一个字符串而不是数组。我怎样才能解决这个问题?另外,请建议一种快速、良好的方式来实现此标题索引。

如果您想创建一个名列表,请尝试此方法

在接口处:

@property(nonatomic, strong)NSMutableDictionary *dict;
@property(nonatomic, strong)NSMutableArray *alphabet;

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

在Datasource方法中,您必须像这样返回它

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

如果这个适合你,请试一试

更新:


如果您还想对数组进行排序,我建议您先按firstName对eventlist进行排序!因此,当您在eventStatusList上循环时,您有正确的顺序。

在第三行中,您将
((OCEventStatus*)eventStatusList[i])user.firstName设置为对象。我猜
firstName
是字符串而不是数组。你可以再发一些代码吗?我认为你为firstchar符号设置了错误的对象。不,设置正确。但这是一根绳子。它应该是一个名字数组。可以吗?当然可以,但首先你必须创建这个数组。。。您正在使用数组创建字典。但是我怎样才能得到每个部分的数据呢。作为-->Ads、Awew B-->Ben、Bret、Ball-(NSInteger)tableView的示例:(UITableView*)tableView行数部分;(NSInteger)部分{return[dict objectForKey:[alphabet objectAtIndex:indexath.section]objectAtIndex:indexath.row];}No
indexath
这个方法的定义听到这个消息很高兴!当然,在这个方法中并没有indexPath参数。。。我写下来时没有xcode;-)
@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];
    }
}
-(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;