【问题标题】:Saving Custom UITableView detail views with SQlite?使用 SQlite 保存自定义 UITableView 详细视图?
【发布时间】:2011-11-08 15:52:13
【问题描述】:

我需要一些帮助来保存自定义细节视图控制器。在我的表格视图中,我有一个添加和编辑按钮,用户可以在其中添加和删除自定义单元格。每个单元格都进入同一个自定义视图,我唯一的问题是有什么方法可以使用 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];
        }

【问题讨论】:

    标签: database xcode sqlite tableview detailsview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多