gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:22:08 24 4
gpt4 key购买 nike

我正在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com