Reputation:
I am having table view in my iPhone application. The requirement is that I have to use gestures for that table view so that when user tap+hold on a row then a dialogue box should come up confirming user to save the data of that specific row to iPhone core data or nsuserdefaults.
I want to know that what option would be the best to save the data i.e., core data or nsuserdefaults?
And the most important How can I achieve it?
I need any example or code snippet to get accomplish this...
thanx
Upvotes: 0
Views: 286
Reputation: 999
I would go for NSUserDefault if I am sure that will be no more than 10-20 rows of data, otherwise I would go for CoreData so it can be scaled in the future. I would use the LongPressGestureRecognizer in the cell to get the indexPath of the cell and display the dialog as shown in sample code below.
- (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];
}
Upvotes: 0
Reputation: 1897
I think if we're talking about really a lot of date, you would better use core data. If you only neet to store very little information, such as user settings or something like that NSUserDefaults
is the right way to go.
How to achieve that:
You ne a NSTimer as aproperty, i just called it yourTimer
In your ViewController with the tableView you will need to implement the following methods:
- (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];
}
}
I hope you get it with this help.
Upvotes: 0
Reputation: 384
In my opinion
Small amounts of data: NSUserDefaults
Moderate amounts: plists
Large amounts: CoreData,sqlite
First, let’s take a look at using SQLite directly.
Now at Core Data:
Now the real question is, which is easier to use? Well, that really depends on what you’re doing. In an app I recently created all I had to do was read small amounts of data sequentially from a SQLite database, since this is such a simple task I simply used FMDB along with iPhone SQLite. Now, if I needed to do anything with the data beyond reading it, Core Data can make all this much easier, and I’d recommend using it.
Conclusion: Core Data just makes so many things so much easier that I would recommend using it unless you already have existing code, or are doing only the most basic database usage
here is a list of some tutorials
http://maniacdev.com/2010/04/great-beginners-core-data-tutorial/
http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/
Upvotes: 3