【问题标题】:How to create table view controller from filtered arrays in Xcode?如何从 Xcode 中的过滤数组创建表视图控制器?
【发布时间】:2014-03-10 19:35:47
【问题描述】:

我目前有 2 个表格视图。一个是基本的,它显示每个单元格,如果选中,它会导航到详细视图控制器。

我创建了一个新的 ViewController 来创建过滤的页面视图。我用 plist 填充我的页面视图。我设法创建了一个过滤器,但我不知道如何从这里开始。

这是我的代码:

- (IBAction)ingredientsAddButton:(UIButton *)sender 
{
    int j=0;
    onemsiz.text=ingredientTextField.text;
    ingredientText=onemsiz.text;
    NSLog(ingredientText);
    NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];
    NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path];
    if (arrayOfPlist==NULL) {

    }else {
        for (int i=0; i<4; i++) { 
            // i currently have 4 element is plist.
            NSString *strCurrentRecipeIngredients = [[arrayOfPlist objectAtIndex:i] objectForKey:@"recipeIngredients"];
            NSString *strCurrentRecipeName = [[arrayOfPlist objectAtIndex:i] objectForKey:@"recipeName"];
            //NSLog(@"%d. Loop \n", i+1);
            if([strCurrentRecipeIngredients rangeOfString:(@"%@",ingredientText)].location!=NSNotFound)
            {
                NSLog(@"%@ contains %@ ",strCurrentRecipeName, ingredientText);
                NSLog(ingredientText);
                NSLog(strCurrentRecipeIngredients);
                j++;
            }else {
                NSLog(@"Not found");
                NSLog(ingredientText);
                NSLog(strCurrentRecipeIngredients);
            }
            if (ingredientText==NULL) {
                NSLog(@"empty input");
            }else {

            }
        }
    }
    NSLog(@"%d",j);
}

我的第一个问题是如何在表格视图中显示结果?如果你愿意,我可以提供一些截图。

【问题讨论】:

标签: ios objective-c cocoa-touch uipageviewcontroller


【解决方案1】:

您可以添加 bool ivar,它会告诉您何时需要显示过滤后的数组和新数组,它将显示过滤后的数据:

BOOL isShowFilteredData;
NSArray *filteredData;

在 init 或 viewDidLoad 中将 bool 初始化为 false(你不想显示所有数据);

isShowFilteredData = NO;

您必须更改数据源/委托方法才能使用正确的数据:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return isShowFilteredData ? isShowFilteredData.count : myDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //configure cell, etc..
    //your code 
    if (isShowFilteredData) 
    {
         // show filtered data
         id obj = filteredData[indexPath.row];
         // do something with result
    }
    else
    {
        // show all data
    }
}

现在,当你想显示过滤后的数据时,只需将 ivar 更改为 true,初始化 filtersData 数组并用你要显示的数据填充它并调用 reloadData:

isShowFilteredData = YES;
filteredData = [[NSArray alloc] initWithObjects:.....];
[self.tableView reloadData];

但如果您想显示所有数据,请执行以下操作:

isShowFilteredData = NO;
[self.tableView reloadData];

这只是您可以使用的解决方案之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多