【问题标题】: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
【解决方案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];
}
}
我希望你能在这个帮助下得到它。