首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从UILongPressGestureRecognizer上的表视图单元格保存数据

从UILongPressGestureRecognizer上的表视图单元格保存数据
EN

Stack Overflow用户
提问于 2012-01-12 15:10:51
回答 1查看 1.3K关注 0票数 1

我想从UILongPressGestureRecognizer事件的单元格中获取并保存数据。我正在尝试的是,当用户点击并按住很长时间,然后将打开一个对话框(将有3个或更多的按钮),从那里用户将有选择保存特定的单元格数据,或从表格中删除该单元格或转到另一个屏幕。

下面是我用于此目的的代码:

代码语言:javascript
运行
复制
-(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] autorelease];
    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
    pressRecongnizer.minimumPressDuration = 0.5f;
    [cell addGestureRecognizer:pressRecongnizer];
    [pressRecongnizer release];
}

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    cell.textLabel.text = 
    [self.filteredListItems objectAtIndex:indexPath.row];
}
else{
    cell.textLabel.text =
    [self.groups objectAtIndex:indexPath.row];
}

return cell;}

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add to Favourites", @"Take to Map", @"Delete" ,nil] ;
[alert show];}

这里我想知道如何将数据保存到我的coreData中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-12 15:16:59

UIGestureRecognizer有一个视图属性,表示它附加到的视图。

代码语言:javascript
运行
复制
UITableViewCell *cell = (UITableViewCell *)[recognizer view];
NSString *text = cell.textLabel.text;

由于您在每个单元格上放置了一个手势识别器,因此可以很容易地使用上面的代码来获取特定的单元格。

请注意,您必须实现UIAlertDelegate方法并将数据临时保存在某个地方,因为用户选择的任何选项都将反映在一个单独的方法中。

编辑:

由于用户在单元格中的选择是以不同的方法给出的,因此您必须保存对单元格的引用(是否创建indexPath的实例变量、单元格等,这取决于您)。

代码语言:javascript
运行
复制
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {
     UITableViewCell *cell = (UITableViewCell *)[recognizer view];

     self.myText = cell.textLabel.text;
     self.currentCellIndexPath = [self.tableView indexPathForCell:cell];

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add to Favourites", @"Take to Map", @"Delete" ,nil] ;
     [alert show];
 }

要删除单元格,首先需要将其从数据源中删除。至此,您已处于委托方法中:

代码语言:javascript
运行
复制
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Delete"]) {
    [myArray removeObjectAtIndex:self.currentCellIndexPath]; // in this example, I saved the reference to the cell using a property

    // last line of example code
}

现在,您需要使用以下两种方法之一来更新表视图。您可以通过调用以下命令立即刷新表视图:

代码语言:javascript
运行
复制
[self.tableView reloadData];

或者,如果您想要漂亮的删除动画表视图,您可以使用:

代码语言:javascript
运行
复制
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.currentCellIndexPath] withRowAnimation:UITableViewRowAnimationFade];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8831355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档