如何检测单元格是否被选中但未在UITableViewController中单击

Jim*_*mmy 3 iphone objective-c cell ios

在此输入图像描述

我有两个UITableViewCells的设计,一个用于选定的单元格,另一个用于未选择的单元格.我可以检测到在这些事件中选择和取消选择了一个单元格来修改它们的设计:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

但是当用户点击并按住一个Cell时,单元格会突出显示,但这些事件都没有被激活,所以我无法正确重绘UITextLabels的阴影,因为UITitleLabels有方法:titleLabel.highlightedTextColor但不是方法titleLabel.highlightedShadowColor.

在图像中:

1 - Unselected cell
2 - Selected cell
3 - Tap & hold cell, with ugly shadows.
Run Code Online (Sandbox Code Playgroud)

如何检测用户点击并按住单元格?

Mat*_*Mat 7

你可以使用UILongPressGestureRecognizer这样的:

将手势添加到您的手机中 cellForRowAtIndexPath:

UILongPressGestureRecognizer *twoSecPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
            [twoSecPress setMinimumPressDuration:2];
            [cell addGestureRecognizer: twoSecPress];
            [twoSecPress release];
Run Code Online (Sandbox Code Playgroud)

处理选择器

-(void) handlePress:(UILongPressGestureRecognizer *)recognizer {
 if (recognizer.state == UIGestureRecognizerStateBegan) {
        UITableViewCell *cellView=(UITableViewCell *)recognizer.view;
        //do your stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

(未经测试).