Ios 如何增加.csv中的行数
我正在用Xcode 5编写一个iPhone应用程序,从.csv文件中读取总统名单。我还阅读了文件中的其他信息,如聚会和从办公室到办公室的日期 然后,我在TableViewController中显示名称,当用户单击名称时,他们会在UIViewController中看到详细信息 然而,我想做如下事情。假设在TableViewController之前有一个UIViewController,用户可以选择只查看例如民主党总统,然后我需要循环查看.csv文件,只从.csv读取这些文件,并将它们显示在TableViewController上 我在我的总统行数组中尝试了for循环,但它只是在第一行(即.csv文件中的第一位总统)上不断迭代 这是我试过的。首先,viewDidLoad将从.csv文件中读取(可以假设这是按照我测试的预期工作的) 在我的牢房里,我试着:Ios 如何增加.csv中的行数,ios,objective-c,csv,Ios,Objective C,Csv,我正在用Xcode 5编写一个iPhone应用程序,从.csv文件中读取总统名单。我还阅读了文件中的其他信息,如聚会和从办公室到办公室的日期 然后,我在TableViewController中显示名称,当用户单击名称时,他们会在UIViewController中看到详细信息 然而,我想做如下事情。假设在TableViewController之前有一个UIViewController,用户可以选择只查看例如民主党总统,然后我需要循环查看.csv文件,只从.csv读取这些文件,并将它们显示在Tabl
- (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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
//配置单元格。。。
主席*p=(主席*)[self.importedRows objectAtIndex:indexath.row];
for(int i=0;i
但正如我所说,当我向cellForRowAtIndexPath添加断点时,for()循环只会在第一行重复:
self.importedRows.count
有谁能告诉我如何增加行数,以便我可以逐个遍历.csv文件中的所有行,以选择要显示在TaleViewContoller上的正确值吗?您不能在
cellForRowAtIndexPath
中过滤或限制表视图
您应该创建一个过滤数组self.filteredRows
,该数组只包含要显示的对象(例如在viewDidLoad
中)。然后使用self.filteredRows
而不是
self.importedRows
在所有表视图数据源方法中。之所以只看到循环中的第一行,是因为正在检查的行(President*p)在循环开始之前已初始化
看看为什么在for循环体中从未提到变量“i”?这是循环中唯一可以改变的东西
正确的答案是采纳马丁纳的建议(他错了,你不能在CellForRowatineXpath中过滤,但正确的是,这是一个糟糕的想法)
现在,每当用户界面设置self.party=@“辉格党”代码>,将更新该表。让numberOfRowsInSection answerself.presidents.count
并在CellForRowatinex路径中,使用self.presidents[indexPath.row]
配置单元格
- (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;
}
@property(nonatomic, strong) NSArray *presidents;
@property(nonatomic, strong) NSString *party;
- (void)setParty:(NSString *)party {
_party = party;
_presidents = nil;
[self.tableView reloadData];
}
- (NSArray *)presidents {
if (!_presidents) {
NSPredicate *partyPredicate = [NSPredicate predicateWithBlock:^BOOL(President *president, NSDictionary *bind) {
return [president.party isEqualToString:self.party];
}];
_presidents = [self.importedRows filteredArrayUsingPredicate:partyPredicate];
}
return _presidents;
}