首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从用户中识别超过5秒的UITableView单元触屏

如何从用户中识别超过5秒的UITableView单元触屏
EN

Stack Overflow用户
提问于 2013-01-02 13:04:54
回答 1查看 1.6K关注 0票数 0

我正在创建一个应用程序,在那里我显示了一些rss内容从url为我的UITableView,直到这里,它的工作完美。但在这里,我想要的是,当用户单击并在UITableViewCell上停留超过5秒时,可以执行其他功能。我只想知道一个单一的选择,点击和保持选择的UITableViewCell。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-01-02 13:07:57

您必须向单元格中添加一个UILongPressGestureReconizer,并将其设置为minimumPressDuration为5。

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 5.0;
        [cell addGestureRecognizer:longPress];

    }

    // Do additional setup

    return cell;
}

编辑:

请注意,如果使用的是prototypeCells,则!cell条件永远不会为真,因此您必须编写如下所示的内容:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    if (cell.gestureRecognizers.count == 0) {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 5.0;
        [cell addGestureRecognizer:longPress];
    }
    return cell;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14122598

复制
相关文章

相似问题

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