我有一个包含一些名称的plist文件,我为部分的磁贴创建了一个NSMultipleArray
,它是字母。我从 plist 文件中获取名称:
- (void)viewDidLoad {
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"NameList" ofType:@"plist"] ;
namesArray = [NSArray arrayWithContentsOfFile:filePath];
//create alphabetic letters
alphabetsSection = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
}
表视图设置:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return alphabetsSection.count;
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return (NSString*)[alphabetsSection objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
return index;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [alphabetsSection objectAtIndex:section]]];
return sectionArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString*cellIdentifier = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//*** I THIK HERE IS THE PROBLEM ****//
NSString *sectionTitle = [alphabetsSection objectAtIndex:indexPath.section];
cell.DCName.text = [dinoNamesArray objectAtIndex:indexPath.row];
return cell;
}
问题在于只有字母 A 的名称才会显示在部分中。我不知道如何将namesArray
添加到sectionTitle
.
这是使代码正常工作的快速修复方法。 在cellForRowAtIndexPath
:
NSString *sectionTitle = [alphabetsSection objectAtIndex:indexPath.section];
NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", sectionTitle]];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];
但是这将在显示每一行时过滤数组,这会很慢:最好在首次加载视图时只对每个部分执行一次过滤。 大致如下:
- (void)viewDidLoad {
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"NameList" ofType:@"plist"] ;
namesArray = [NSArray arrayWithContentsOfFile:filePath];
//create alphabetic letters
alphabetsSection = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
// create a corresponding array holding the objects for each section
arrayForSection = [NSMutableArray array]
for (NSString *letter in alphabetsSection) {
NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", letter]];
[arrayForSection addObject:sectionArray];
}
}
(您需要添加一个属性NSMutableArray *arrayForSection
)。
这将构建一个数组,arrayForSection
,其每个元素都是一个数组,其中包含属于表相应部分的对象。 因此,您可以修改数据源方法以使用此数组:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *sectionArray = arrayForSection[section];
return sectionArray.count;
}
在cellForRowAtIndexPath
:
NSArray *sectionArray = arrayForSection[indexPath.section];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];
=> 请参阅下面的帖子,这将有助于您更正:-
如何将 Plist 数据读取到 UITableView