ios - UITableView 字母顺序有问题

标签 ios objective-c iphone uitableview

我有一个包含一些名称的 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/

相关文章:

iphone - 有没有办法查明 iPhone 是否通过 A-GPS、手机信号塔信号强度或 wifi 获得了 CLLocation?

ios - UITextField文字颜色透明

ios - iOS (iPhone/iPad) 中的串行或 HID (usb) 通信

objective-c - objective-c 条件语句评估

ios - 无法使 UILabel 文本发光

ios - UIStackView 计算错误的 UIImageView 高度

iphone - 如何在 Xcode 中添加两个编译器标志

ios - 在 segue 中与自定义过渡一起设置动画状态栏

objective-c - 对 "touch"事件执行几乎相同的操作(针对不同的图像)?

iphone - 自动引用计数不释放 calloc