问题描述
我有以下数据结构,我无法更改(可能是这个问题最重要的部分):
I have the following data structure, which I cannot change (probably the most important part of this question):
<?xml version="1.0" encoding="us-ascii"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>FirstName</key>
<string>John</string>
<key>LastName</key>
<string>Adams</string>
</dict>
<dict>
<key>FirstName</key>
<string>Henry</string>
<key>LastName</key>
<string>Ford</string>
</dict>
</array>
</plist>
我可以成功地将其读入 Person
(我创建的)类类型的 NSArray
并在 UITableView
中显示此列表>.
I can successfully read this into an NSArray
of class type of Person
(which I created) and well as show this list in a UITableView
.
我现在想对这些数据做的是按部分显示,按姓氏的第一个字母显示,并显示SectionIndexList
.
What I would like to do with this data now is show it in sections, by the first letter in their last name as well as show the SectionIndexList
.
我如何转换这些数据(不是数据源),或者保持原样并直接在我的 DataSource
中为 UITableView
查询它,以便我可以通过他们姓氏的第一个字母?
How can I transform this data (not the data source), or leave it intact and query it directly in my DataSource
for UITableView
so I can section it off by the first letter in their last name?
提前致谢.
推荐答案
你应该这样做:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"name_of_plist" ofType:@"plist"];
NSArray *personsFileArray = [NSArray arrayWithContentsOfFile:filePath];
// At this point what you have inside personsFileArray are NSDictionaries as defined in your plist file. You have a NSArray<NSDictionary*>.
NSMutableDictionary *indexedPersons = [[NSMutableDictionary alloc] init];
// I am assuming you have a class called Person
for each (NSDictionary *d in personsFileArray) {
Person *p = [[Person alloc] initWithDictionary:d];
NSString *firstLetter = [p.lastName substringToIndex:1];
NSMutableArray *persons = indexedPersons[firstLetter];
if (!persons) {
persons = [[NSMutableArray alloc] init];
}
[persons addObject:p];
[indexedPersons setObject:persons forKey:firstLetter];
}
// After this, you have a dictionary indexed by the first letter, and as key an array of persons.
// Now you need to implement UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *firstLetter = [self.indexedPersons allKeys][section];
return self.indexedPersons[firstLetter].count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.indexedPersons allKeys].count;
}
并为章节索引标题实现此方法;
And implement this methods for the section index titles;
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
如果您有任何疑问,这里有很多教程:
If you have any doubts, there are plenty of tutorials:
http://www.appcoda.com/ios-programming-index-list-uitableview/
希望能帮到你!!
这篇关于UITableView 从 NSArray 动态创建部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!