Swift UITable下拉刷新崩溃应用程序

Swift UITable下拉刷新崩溃应用程序,swift,uitableview,pull-to-refresh,Swift,Uitableview,Pull To Refresh,一切似乎都加载正确,但当我在我的应用程序中下拉我的表视图时,它崩溃了,我不知道为什么。我丢失了一段代码吗 以下是我的viewController代码: import UIKit import Firebase import FirebaseDatabase class FindPartiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // where we will st

一切似乎都加载正确,但当我在我的应用程序中下拉我的表视图时,它崩溃了,我不知道为什么。我丢失了一段代码吗

以下是我的viewController代码:

import UIKit
import Firebase
import FirebaseDatabase

class FindPartiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // where we will store all the parties
    var parties = [party]()

    @IBOutlet weak var partyTable: UITableView!

    var refreshControl = UIRefreshControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        partyTable.delegate = self
        partyTable.dataSource = self
设置刷新控件:

        self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        self.refreshControl.addTarget(self, action: Selector(("refresh:")), for: UIControlEvents.valueChanged)
        self.partyTable?.addSubview(refreshControl)

        let ref = FIRDatabase.database().reference()

        // go to firebase and get all the parties and all there info
        ref.child("parties").queryOrderedByKey().observe(.childAdded, with: {
            snapshot in
            let snapshotValue = snapshot.value as? NSDictionary
            let name = snapshotValue?["name"] as! String
            let location = snapshotValue?["location"] as! String
            let description = snapshotValue?["description"] as! String
            let upVotes = snapshotValue?["upVotes"] as! String
            let downVotes = snapshotValue?["downVotes"] as! String

            self.parties.append(party(name: name, description: description, location: location, upVotes: Int(upVotes)!, downVotes: Int(downVotes)!))

            // update the table view to the all the parties
            DispatchQueue.main.async {
                self.refreshTableData()
            }
        })

    }

    // refreshes the table data when called
    func refreshTableData(){
        // go thru all the parties ansd sort them from postive to negative total votes
        for i in (0 ..< self.parties.count) {
            let total = self.parties[i].upVotes - self.parties[i].downVotes
            if i != 0{
                let oldTotal = self.parties[i-1].upVotes - self.parties[i-1].downVotes
                if total > oldTotal {
                    let savedParty = self.parties[i]
                    self.parties.remove(at: i)
                    self.parties.insert(savedParty, at:i-1)
                }
            }
        }

        self.partyTable?.reloadData()
    }

    // refreshes the table data when the table is pulled down
    func refresh(sender:AnyObject) {
        print("REFRESHED")
        refreshTableData()
    }
更新:

我收到的错误消息是:

2016-11-07 19:00:58.625 Lit[6530:1248106] -[Lit.FindPartiesViewController refresh:]: unrecognized selector sent to instance 0x7fe618d2e230
2016-11-07 19:00:58.638 Lit[6530:1248106] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Lit.FindPartiesViewController refresh:]: unrecognized selector sent to instance 0x7fe618d2e230'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e82634b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010e28721e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e895f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010e7abc15 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010e7ab798 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ec4bb88 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKit                               0x000000010edd12b2 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010edd15cb -[UIControl _sendActionsForEvents:withEvent:] + 444
    8   UIKit                               0x000000010f6e9dac -[UIRefreshControl _setRefreshControlState:notify:] + 525
    9   UIKit                               0x000000010f6f2231 -[_UIRefreshControlModernContentView _snappingMagic] + 53
    10  UIKit                               0x000000010f6f0c70 -[_UIRefreshControlModernContentView didTransitionFromState:toState:] + 205
    11  UIKit                               0x000000010f6e9d3d -[UIRefreshControl _setRefreshControlState:notify:] + 414
    12  UIKit                               0x000000010f6e8b18 -[UIRefreshControl _setVisibleHeight:] + 71
    13  UIKit                               0x000000010f6e91ea -[UIRefreshControl _update] + 374
    14  UIKit                               0x000000010ed33f5a -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 174
    15  UIKit                               0x000000010ed1e919 -[UIScrollView setContentOffset:] + 478
    16  UIKit                               0x000000010eda96b7 -[UITableView setContentOffset:] + 316
    17  UIKit                               0x000000010ed23351 -[UIScrollView _updatePanGesture] + 2346
    18  UIKit                               0x000000010f18c289 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    19  UIKit                               0x000000010f194028 _UIGestureRecognizerSendTargetActions + 109
    20  UIKit                               0x000000010f191af7 _UIGestureRecognizerSendActions + 227
    21  UIKit                               0x000000010f190d83 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 891
    22  UIKit                               0x000000010f17ce56 _UIGestureEnvironmentUpdate + 1395
    23  UIKit                               0x000000010f17c89b -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 521
    24  UIKit                               0x000000010f17ba7e -[UIGestureEnvironment _updateGesturesForEvent:window:] + 286
    25  UIKit                               0x000000010ecba7ad -[UIWindow sendEvent:] + 3989
    26  UIKit                               0x000000010ec67a33 -[UIApplication sendEvent:] + 371
    27  UIKit                               0x000000010f459b6d __dispatchPreprocessedEventFromEventQueue + 3248
    28  UIKit                               0x000000010f452817 __handleEventQueue + 4879
    29  CoreFoundation                      0x000000010e7cb311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    30  CoreFoundation                      0x000000010e7b059c __CFRunLoopDoSources0 + 556
    31  CoreFoundation                      0x000000010e7afa86 __CFRunLoopRun + 918
    32  CoreFoundation                      0x000000010e7af494 CFRunLoopRunSpecific + 420
    33  GraphicsServices                    0x00000001121afa6f GSEventRunModal + 161
    34  UIKit                               0x000000010ec49f34 UIApplicationMain + 159
    35  Lit                                 0x000000010c419b3f main + 111
    36  libdyld.dylib                       0x0000000110e6d68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

由于您没有使用发件人,请将其删除

func refresh() {
     print("REFRESHED")
     refreshControl?.endRefreshing()
     refreshTableData()
}
然后,在将目标添加到刷新控件时,不要在
刷新
选择器之后使用
(因为您删除了sender参数)

另外,通过使用
#选择器(刷新)
而不是
选择器(“刷新”)
,您将知道在编译之前是否识别选择器

self.refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)

由于您没有使用发件人,请将其删除

func refresh() {
     print("REFRESHED")
     refreshControl?.endRefreshing()
     refreshTableData()
}
然后,在将目标添加到刷新控件时,不要在
刷新
选择器之后使用
(因为您删除了sender参数)

另外,通过使用
#选择器(刷新)
而不是
选择器(“刷新”)
,您将知道在编译之前是否识别选择器

self.refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)

你的功能看起来不错。我认为问题在于您没有正确使用
UIRefreshControl

UIRefreshControl
仅应与
UITableViewController
一起使用,而不是与表视图一起设置的
UIViewController
一起使用。从
UIRefreshControl
的文档中:

注意

由于刷新控件是专门为在由表视图控制器管理的表视图中使用而设计的,因此在不同的上下文中使用它可能会导致未定义的行为


这就是您正在经历的:未定义的行为。

您的函数看起来很好。我认为问题在于您没有正确使用
UIRefreshControl

UIRefreshControl
仅应与
UITableViewController
一起使用,而不是与表视图一起设置的
UIViewController
一起使用。从
UIRefreshControl
的文档中:

注意

由于刷新控件是专门为在由表视图控制器管理的表视图中使用而设计的,因此在不同的上下文中使用它可能会导致未定义的行为



这就是您正在经历的:未定义的行为。

尝试设置断点,以确定哪一行(如果是代码行,则可能是哪一行)导致崩溃。请发布完整的错误,它们旨在帮助您了解发生的情况。您正在强制取消包装这么多东西,难怪它会崩溃。@ohr添加了错误这是您的错误“self.refreshControl.addTarget(self,action:Selector((“refresh:”)),for:uicontrol事件.valueChanged)”这个“refresh:”方法存在吗?它有输入错误吗?@ohr有什么问题吗?试着设置断点以找出哪一行(如果是代码行,那么可能是哪一行)导致了崩溃。请发布您的完整错误,它们旨在帮助您了解发生了什么。您正在强制取消包装这么多东西,难怪它会崩溃。@ohr添加了错误这是您的错误“self.refreshControl.addTarget(self,action:Selector((“refresh:”)),for:uicontrol事件.valueChanged)”这个“refresh:”方法存在吗?它有输入错误吗?@ohr有什么问题吗?所以我应该用上面的一个替换我的self.refreshControl.addTarget()?是的,尝试使用
#selector(self.refresh)
而不是
selector(“refresh:”)
,并从refresh方法中删除sender参数。好的,现在它不会崩溃,但是“pull to refresh”spinner不会将
refreshControl?.endRefresh()
添加到您的刷新方法中。只是一个小细节,但是很难让刷新器显示在表格单元格后面而不是顶部吗?所以我应该用上面的方法替换我的self.refreshControl.addTarget()?是的,尝试使用
选择器(self.refresh)
而不是
选择器(“refresh:”)
并从refresh方法中删除sender参数。好的,现在它不会崩溃,但是“pull to refresh”和微调器不会进入endAdd
refreshControl?.EndRefresh()
到你的refresh方法。只是一个小细节,但是,要让进修人员出现在表格单元格后面而不是顶部是否很困难?