0

所以,我的一些用户没有意识到他们可以按住一个单元格来获得带有选项的警报弹出窗口。所以,我想添加某种图像、文本或颜色——一些东西——让用户知道“嘿,你可以拿着这个来获取更多信息。” 下面是我的代码。对最好的方法有什么想法吗?

// handles long press for editing or sharing a search
func tableViewCellLongPressed(
    sender: UILongPressGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.Began &&
            !tableView.editing {
                let cell = sender.view as! UITableViewCell // get cell
                if let indexPath = tableView.indexPathForCell(cell) {
                    displayLongPressOptions(indexPath.row)
                }
        }
}

// displays the edit/share options
func displayLongPressOptions(row: Int) {
    // create UIAlertController for user input
    let alertController = UIAlertController(title: "Helpful Stuff",
        message: "",
        preferredStyle: UIAlertControllerStyle.Alert)

    // create Cancel action
    let cancelAction = UIAlertAction(title: "Cancel",
        style: UIAlertActionStyle.Cancel, handler: nil)
    alertController.addAction(cancelAction)

    let shareAction = UIAlertAction(title: "Share",
        style: UIAlertActionStyle.Default,
        handler: {(action) in self.shareSearch(row)})
    alertController.addAction(shareAction)
    presentViewController(alertController, animated: true,
        completion: nil)
}

// callback that returns a configured cell for the given NSIndexPath
override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell {

        // get cell
        let cell = tableView.dequeueReusableCellWithIdentifier(
            "Cell", forIndexPath: indexPath) as UITableViewCell

        // set cell label's text to the tag at the specified index
        cell.textLabel?.text = model.tagAtIndex(indexPath.row)

        // set up long press guesture recognizer
        let longPressGestureRecognizer = UILongPressGestureRecognizer(
            target: self, action: "tableViewCellLongPressed:")
        longPressGestureRecognizer.minimumPressDuration = 0.5
        cell.addGestureRecognizer(longPressGestureRecognizer)

        return cell
}
4

1 回答 1

0

如果您想使用情节提要,可能更容易连接手势识别器并将其链接到情节提要中的按钮,而不是将其放在多次调用的 cellForRowAtIndexPath 中

于 2016-02-16T19:59:33.327 回答