我有一个包含一些名称的 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];
}
表格 View 设置:
- (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];
但这会在显示每一行时过滤数组,这会很慢:最好在第一次加载 View 时对每个部分只过滤一次。沿着这些线的东西:
- (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];
关于ios - UITableView 字母顺序有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35970891/