我有一个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];
}
TableView设置:
- (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
。
答案 0 :(得分:1)
这是让您的代码正常运行的快速解决方法。在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];
答案 1 :(得分:0)
=>请参阅下面的帖子,这将有助于您进一步澄清: -