簡體   English   中英

滑動單元格時,有沒有一種方法可以從tableViewCell導航到另一個視圖控制器?

[英]Is there a way to navigate from a tableViewCell to another view controller when a cell is swiped?

因此,這就是我制作滑動單元格的方式: 自定義表格視圖單元格不滑動 無論如何,我想知道是否有一種方法可以在向左滑動單元格時從表視圖轉換到另一個視圖控制器。 我知道可以完成以下操作:輕按一個單元格並在didSelectRowAtIndexPath中調用performSegueWithIdentifier,但從未見過何時滑動單元格的示例。 我嘗試將self.performSegueWithIdentifier("detail", sender: self)到滑動邏輯中

if recognizer.state == .Ended {
            // the frame this cell had before user dragged it
            let originalFrame = CGRect(x: 0, y: frame.origin.y,
                width: bounds.size.width, height: bounds.size.height)
            if !reserveOnDragRelease {
                // if the item is not being deleted, snap back to the original location
                UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
            } else {
                UIView.animateWithDuration(0.2, animations: {self.frame = CGRectMake(-self.bounds.size.width, originalFrame.origin.y, originalFrame.width, originalFrame.height)})
            // Added it here 

            }
        }

但是出現了錯誤,即CustomTableViewCell沒有名為performSegueWithIdentifier的成員。 有人有見識嗎? 謝謝!

首先, performSegueWithIdentifierUIViewController方法,因此UIView對象將永遠無法接收它。

其次,我不建議您在滑動時切換View Controller,因為這違反了iOS人機界面原則。 人們習慣於滑動以刪除或編輯它,而不是導航到下一個視圖控制器。

但是,如果您確實確定要執行此操作,只需獲取當前的UIViewController對象即可。 但是我認為將視圖控制器對象放入視圖中並不是一個好的設計。 您可能想看看UITableViewDelegate協議以找到更好的地方。 另外,您可以利用滑動手勢的委托來執行操作。

您不必非要堅持使用performSegueWithIdentifier ,有很多方法可以切換視圖控制器。

這是因為theCustomTableViewCell沒有名為performSegueWithIdentifier的方法,而UIViewController具有該方法

嘗試委派UITableViewDelegate並使用此方法單獨獲取項目:

    myTableView.delegate = self

    ////////////////////////////

    var myItems: [String: Bool] = [:]

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedItem = items.objectAtIndex(indexPath.row) as String
        let itemId = selectedItem.componentsSeparatedByString("$%^")
        // add to self.selectedItems
        myItems[itemId[1]] = true
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedItem = items.objectAtIndex(indexPath.row) as String
        let itemId = selectedItem.componentsSeparatedByString("$%^")
        // remove from self.selectedItems
        myItems[itemId[1]] = nil
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM