所以我在这里有一个小问题,我想我在这篇文章的标题中表达了我的观点。我要排序UITableView
根据我在代码中声明它们的顺序 header 。我已经尝试上网,但找不到任何解决方案。
我认为这里的人可以提供帮助。提前致谢!
编辑:这是我的表格 View 内容的代码
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:accSettingsOpts, @"Account",notifSettingsOpts,@"Notifications",aboutOpts,@"About",logoutOpt,@"Sign Out",nil];
我想显示按排序的表格 View
-Account
-Notifications
-About
-Sign Out
但它显示为
-About
-Account
-Notifications
-Sign Out
编辑:这就是解决问题的方式。
根据我在下面接受的答案,我将其修改为
@interface myClass ()
NSArray *headerArr;
@end
在
viewDidload
我添加了headerArr = [[NSArray alloc]initWithObjects:@"Account",@"Notifications",@"About",@"Sign Out", nil];
最后...
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [headerArr objectAtIndex:section];
}
谢谢你们的帮助。没想到就这么简单。
最佳答案
字典是无序的集合。如果您将项目存储在字典中,则定义它们的顺序不会保存在任何地方。
TableView 是对象的固有有序列表。因此,字典不适合作为存储 TableView 项目的方式。然而,数组是表格 View 的自然数据源。
使用数组而不是字典。就这么简单。您可以使用字典数组或其他一些数据对象的数组,然后编写 cellForRowAtIndexPath 方法从模型(数组元素)中提取数据并将值安装到单元格中。
关于ios - 如何根据声明顺序对 UITableView header 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993077/