基本的IOS数据库应用程序与SQLite3



所以我正在设计一个简单的应用程序来练习我的iOS开发技能,基本上我正在制作一个简单的数据库应用程序,其中包含不同类型的水果和蔬菜。我设法读取整个数据库到我的应用程序,并显示在一个表视图,这是我想要的。然而,我现在看到的是所有的水果和蔬菜一起在主页上,大约有一千种。我想分组列,并移动到下一个表视图,所以,例如,如果我按下水果表行,我将继续到一个新的TableView,将显示所有(和只有)水果,同样的蔬菜等。我知道我应该使用GROUP BY,它将所有独特的食物类型(例如水果,蔬菜,乳制品)分组,但不太确定如何使用它。任何帮助或建议将非常感激。

好的,这可能是你想要的:

首先声明数据数组:

NSMutableArray *datasource;
datasource=[[NSMutableArray alloc]initWithObjects:@"Fruits",@"Vegetables", nil];

设置表的行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [datasource count];
}

输入数据到单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
    cell.textLabel.text=[datasource objectAtIndex:indexPath.row];
    // Configure the cell...
    return cell;
}

你真正需要做的是在preparesegue方法中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DestinationVC *destVC = segue.destinationViewController;
    UITableViewCell *cell=[myTable cellForRowAtIndexPath:path];
    destVC.selectedFoodType=cell.textLabel.text;
    //This will pass what type of food you selected and pass it to the second tableview. Then there you can build the logic to populate table cells according to selected food type

}

注意:通过storyboard连接从cell到另一个视图控制器的segue。我相信你知道怎么做

最新更新