使用 SQlite 保存自定义 UITableView 详细视图?

发布于 2024-12-14 17:17:55 字数 1682 浏览 4 评论 0原文

我需要一些帮助来保存自定义详细视图控制器。在我的表格视图中,我有一个添加和编辑按钮,用户可以在其中添加和删除自定义单元格。每个单元格都会进入相同的自定义视图,我唯一的问题是有什么方法可以使用 SQlite 或其他数据库保存每个单元格?我只是不想创建大量的视图控制器并使用 NSUserDefaults 保存每个单元格。

我的代码:

表。 h =

IBOutlet UITableView *warrantyTableView;

    NSMutableArray *data;
    IBOutlet UITextField *tableCellText;
    IBOutlet UITableView *mainTableView;
    IBOutlet UINavigationItem *navItem;
}

- (IBAction)addRowToTableView;
- (IBAction)editTable;
- (NSString *)dataFilePath;
- (IBAction)endText;  

表.m =

 - (NSString *)dataFilePath {

        NSString *dataFilePath;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [paths objectAtIndex:0];
        dataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
        return dataFilePath;

    }

    - (void)saveData {

        [NSKeyedArchiver archiveRootObject:[data copy]  toFile:[self dataFilePath]];

    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.

         PushedViewController *pushedViewController = [[PushedViewController alloc] initWithNibName:@"PushedView" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
            PushedViewController *ifView = [[PushedViewController alloc] initWithNibName:@"PushedView" bundle:nil];
            [self.navigationController pushViewController:ifView animated:YES];
            [ifView release];
        }

I need some help saving custom detail view controllers. In my table view I have an add and edit button where the user can add and delete custom cells. Each cell goes to the same custom view, my only problem is that is there any way that I can save each cell using SQlite or another Database? I just don't want to have to create a ton of viewcontrollers and save each and every cell using NSUserDefaults.

My code :

table . h =

IBOutlet UITableView *warrantyTableView;

    NSMutableArray *data;
    IBOutlet UITextField *tableCellText;
    IBOutlet UITableView *mainTableView;
    IBOutlet UINavigationItem *navItem;
}

- (IBAction)addRowToTableView;
- (IBAction)editTable;
- (NSString *)dataFilePath;
- (IBAction)endText;  

Table.m =

 - (NSString *)dataFilePath {

        NSString *dataFilePath;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [paths objectAtIndex:0];
        dataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
        return dataFilePath;

    }

    - (void)saveData {

        [NSKeyedArchiver archiveRootObject:[data copy]  toFile:[self dataFilePath]];

    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.

         PushedViewController *pushedViewController = [[PushedViewController alloc] initWithNibName:@"PushedView" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
            PushedViewController *ifView = [[PushedViewController alloc] initWithNibName:@"PushedView" bundle:nil];
            [self.navigationController pushViewController:ifView animated:YES];
            [ifView release];
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2024-12-21 17:18:00

为什么要将每个单元格保存在数据库中?将单元格中的所有数据保存在 SQLite 中。访问时,将 SQLite 数据库中的数据获取到字典数组中。字典将包含一个单元格的所有数据,数组将包含具有每个单元格数据的所有字典。然后使用该数组中的数据重新加载表格视图。 W在表视图中编辑/添加/删除对象时,首先从字典数组中执行整个操作,然后,当单个操作完成时,将其保存到 sqlite 中并再次重新加载表视图。

Why do you want to save each cell in database ? Save all the data in the cells in SQLite. While accessing, get the data from SQLite database to an array of dictionary. The dictionary will contain all data of one cell, the array will contain all the dictionaries having the data of each cell. Then reload the tableview with the data from that array. WWhile editing/adding/deleting objects to the tableview, do the whole operation first from the array of dictionary and then, when a single operation is finished, save it to the sqlite and again reload the tableview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文