ios - 如何增加 .csv 中的行数

标签 ios objective-c csv

我正在 Xcode 5 中开发一个 iPhone 应用程序,我从 .csv 文件中读取总统姓名列表。我还从文件中读取了其他信息,例如聚会和任职日期。

然后我在 TableViewController 中显示姓名,当用户单击姓名时,他们会在 UIViewController 中看到总统的详细信息。

不过,我想做如下的事情。想象一下,在 TableViewController 之前有一个 UIViewController,用户可以在其中选择仅查看,例如民主党总统,然后我需要遍历 .csv 文件,只从 .csv 中读取这些文件并将它们显示在 TableViewController 上。

我在我的总统行数组上尝试了一个 for 循环,但它只是不断迭代第一行(即 .csv 文件中的第一位总统)。

这是我尝试过的。首先,要从 .csv 文件中读取的 viewDidLoad(可以假设这是按我测试过的预期工作的)。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSError *outError = nil;
    NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"USPresidents"  ofType:@"csv"];

    NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:&outError];
    self.importedRows = [self csvArray2PresidentsArray:[fileString csvRows]];

    // Prints a list of all the presidents - working as intended
    NSLog(@"Filestring: %@", fileString);
}

在我的 cellForRowAtIndexPath 中,我尝试了:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];

    for (int i = 0; i < self.importedRows.count; i++)
    {
        if ([p.party isEqualToString:@"Democratic-Republican"])
        {
            NSLog(@"President Name: %@ , President Party: %@", p.name, p.party);
            NSString *tempString;
            tempString = p.party;
            NSLog(@"TempString: %@", tempString);
            cell.textLabel.text = [NSString stringWithFormat:@"%@", tempString]; 
        }
    }
    return cell;
}

但正如我所说,当我向 cellForRowAtIndexPath 添加一个断点时,for() 循环只是不断重复第一行: self.importedRows.count

谁能告诉我如何增加行数,以便我可以一个接一个地遍历 .csv 文件中的所有行,以选择正确的值显示在 TaleViewContoller 上?

最佳答案

您不能过滤或限制 cellForRowAtIndexPath 中的 TableView 。

您应该创建一个过滤数组 self.filteredRows,它只包含您想要显示的对象(例如在 viewDidLoad 中)。然后使用 self.filteredRows 而不是 self.importedRows 在所有 TableView 数据源方法中。

关于ios - 如何增加 .csv 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059066/

相关文章:

objective-c - 使用 valueForKeyPath 获取数组元素

ios - Swift 将字典数组传递给 NSMutableArray

Python递归执行try except while条件满足

python - 如何将 .csv 文件中的日期时间字符串列拆分为单独的日期和时间列?

csv - 重建 CSV 并使用字段值作为导出的文件名

ios - 在 swift 中的自定义 View 上播放透明视频(H.264,AAC)

ios - 当我在 swift 框架中使用 Bridging Headers 时失败

iphone - 即使在调用 commitAnimation 之后,动画也会影响每个 View

ios - 为本地 Spotlight 搜索设置关键字

ios - Deezer SDK IOS 按 id 播放轨道