【问题标题】:Saving chosen row data from tableview in iPhone从 iPhone 中的 tableview 保存选定的行数据
【发布时间】:2012-01-04 11:46:23
【问题描述】:

我的 iPhone 应用程序中有表格视图。要求是我必须对该表格视图使用手势,以便当用户点击并按住一行时,应该会出现一个对话框,确认用户将该特定行的数据保存到 iPhone 核心数据或 nsuserdefaults。

我想知道保存数据的最佳选择是核心数据还是 nsuserdefaults?

而最重要的我该如何实现呢?

我需要任何示例或代码 sn-p 来完成此操作...

感谢

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    在我看来

    少量数据:NSUserDefaults

    适量:plists

    大量:CoreData,sqlite

    首先,我们来看看直接使用 SQLite。

    • 关系数据库系统
    • 跨平台兼容性的潜力
    • 许多早期的 iPhone 数据库示例都是使用 SQLite 编写的
    • Objective-C 包装器(例如 FMDB)非常易于使用

    现在在核心数据:

    • 可以以自己的二进制或 SQLite 存储格式存储数据。
    • 可以序列化对象
    • 比直接使用 SQLite 高得多
    • 不是 RDBMS,您实际上可以直接存储内容。

    现在真正的问题是,哪个更容易使用?嗯,这真的取决于你在做什么。在我最近创建的一个应用程序中,我所要做的就是从 SQLite 数据库中顺序读取少量数据,因为这是一项非常简单的任务,我只是将 FMDB 与 iPhone SQLite 一起使用。现在,如果除了读取数据之外我还需要对数据做任何事情,Core Data 可以让这一切变得更容易,我建议使用它。

    结论:Core Data 让很多事情变得如此简单,我建议您使用它,除非您已经拥有现有代码,或者只使用最基本的数据库

    这里是一些教程的列表

    1. http://www.applausible.com/blog/?p=317

    2. http://maniacdev.com/2010/04/great-beginners-core-data-tutorial/

    3. http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/

    【讨论】:

      【解决方案2】:

      如果我确定不会超过 10-20 行数据,我会选择 NSUserDefault,否则我会选择 CoreData,以便将来可以扩展。我将使用单元格中的 LongPressGestureRecognizer 来获取单元格的 indexPath 并显示对话框,如下面的示例代码所示。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      { 
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
                  pressRecongnizer.minimumPressDuration = 0.5f;
                  [cell addGestureRecognizer:pressRecongnizer];
                  [pressRecongnizer release];
          }
          // your cell info
          cell.textLabel.text = @"test";
          return cell;
      } 
      
      - (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer
      {
          // You can access to the tableview cell from the recognizer.view
          // You can get the data directly from the cell data more or label and etc.
          UITableViewCell *cell = (UITableViewCell *)recognizer.view;
      
          // You can either get the NSIndexPath of the cell from the tableview to 
          // access the data model from you data collection.
          NSIndexPath *indexPath = [self.tableview indexPathForCell:cell]
      
          // Use the index path to get data and display dialog.
          // For example:
          UIAlertView *alert = [UIAlertView alloc] – initWithTitle:@"table cell pressed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
          [alert show];
      }
      

      【讨论】:

        【解决方案3】:

        我认为,如果我们要讨论很多日期,您最好使用核心数据。如果您只需要存储很少的信息,例如用户设置或类似的东西,NSUserDefaults 是正确的选择。

        如何实现: 您需要一个 NSTimer 作为属性,我只是将其称为 yourTimer 在带有 tableView 的 ViewController 中,您需要实现以下方法:

        - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
            [yourTimer invalidate];
            yourTimer = nil;
        }
        - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
             [yourTimer invalidate];
             yourTimer = nil;
        }
        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
            UITouch *touch = [touches anyObject];
            CGPoint *currentPosition = [touch locationInView:self.view];
        
            //here you have to check if the touch is inside the cell you wanted
            //note that this code wont work, you have to personalize it, this is only a hint
            if ([currentPosition isInsideYourCell]){
                if (!yourTimer.isValid) {
                    // NSLog(@"testausgabe");
                    yourTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 //any interval you like 
                                                                 target:self 
                                                               selector:@selector(onTick:) 
                                                               userInfo:nil repeats: YES];
                   [yourTimer fire];
                 }
             }
        
        }
        - (BOOL)isInsideYourCell{
        //check if the touch is inside the cell
        }
        - (void)onTick:(NSTimer *)timer {
            if (timer.timeInterval >= 2.0){
                //lets say your dialogue will appear after 2 secconds
                [self displayYourDialogue];
            }
        }
        

        我希望你能在这个帮助下得到它。

        【讨论】:

          猜你喜欢
          • 2013-03-16
          • 1970-01-01
          • 1970-01-01
          • 2017-07-30
          • 1970-01-01
          • 1970-01-01
          • 2021-08-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多