'尝试滚动到无效的索引路径:<nsindexpath:0x6000002264a0 =“”> {length = 2,path = 0 - 0}'

时间:2017-05-19 14:07:48

标签: swift realm

我是Swift and Realm的新手,抱歉。我正在尝试构建一个Scrollable Tab页面,其中所有数据都来自Realm。

首次启动,成功登录后:数据未自动加载。

第二次发布,我收到此错误。

'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x6000002264a0> {length = 2, path = 0 - 0}'

我使用静态字符串数组测试了我的代码,它运行良好。实际上,这个编码也是从某个地方拍摄的......

这是我的ViewController:

class ViewController: UIViewController{

         let tabs = try! Realm().objects(Tabs)

         //after successful login. begin collecting Tab Items
         override func viewDidLoad() {
            WebController().getTabs()
            buildTabItems()
         }

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)

            view.layoutIfNeeded()
            buildSamplePages()

            let firstPageIndex = 2

            focusView.layer.cornerRadius = (scrollingTabBar.height - scrollingTabBar.focusVerticalMargin * 2.0) / 2.0
            scrollView.contentSize = CGSize(width: view.width * CGFloat(tabItems.count), height: scrollView.contentSize.height)
            scrollingTabBar.width = view.width
            scrollingTabBar.pageWidth = scrollView.width
            scrollingTabBar.reloadTabs(tabItems, indexOf: firstPageIndex)
            scrollView.setContentOffset(CGPoint(x: scrollView.width * CGFloat(firstPageIndex), y: scrollView.contentOffset.y), animated: false)

        }


       //collect Tab Items from Realm
       func buildTabItems() {

            let array = Array(tabs).description
            var strings = [String]()

            for tabData in tabs{
                strings.append(tabData.title)
            }


            for str in strings {
                let tabItem = USGScrollingTabItem()
                tabItems.append(tabItem)
            }
        }

       //display Pages
        func buildTabPages() {
            for (pageNum, _) in tabItems.enumerated() {
                let label = UILabel(frame: CGRect.zero)
                label.text = "i am a test\(pageNum)"
                scrollView.addSubview(label)
            }
        }


    }

    extension ViewController: USGScrollingTabBarDelegate {

        func tabBar(_ tabBar: USGScrollingTabBar, didSelectTabAt index: Int) {
            scrollView.setContentOffset(CGPoint(x: scrollView.width * CGFloat(index), y: scrollView.contentOffset.y), animated: true)
        }
    }

    extension ViewController: UIScrollViewDelegate {

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            scrollingTabBar.enabled = !scrollView.isTracking;

            if (scrollView.isTracking || scrollView.isDecelerating) {
                scrollingTabBar.scrollToOffset(scrollView.contentOffset.x)
            }
        }

        func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
            scrollingTabBar.stopScrollDeceleration()
            scrollingTabBar.enabled = false
        }

        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            scrollingTabBar.enabled = true
        }
    }

这是我的WebService代码

class WebController{
    let a = UserDefaults.standard.string(forKey: "a")
    let b = UserDefaults.standard.string(forKey: "b")
    let c = UserDefaults.standard.integer(forKey: "c")
    let d = UserDefaults.standard.string(forKey: "d")

    func  getTabs() {

        let reqParams: Parameters = ["a": a ,"b": b]
        let header = ["c":"c, "d":"d"]
        let url = APIConstant.URLS().TabsURL

        guard let realm = try? Realm() else {
            return
        }

        Alamofire.request(url, method: HTTPMethod.post, parameters:reqParams, headers:header).responseJSON { response in

        guard response.result.isSuccess, let value = response.result.value else {
                return
            }
            let json = JSON(value)
            let tabs = json["result"]
            realm.beginWrite()
            for (_, subJson) : (String, JSON) in tabs {
                let tabs: Tabs = Mapper<Tabs>().map(JSONObject: subJson.dictionaryObject!)!
                realm.add(tabs, update: true)
            }
            do {
                try realm.commitWrite()
            } catch {

            }
        }

    }

}

任何人都可以给我一个想法,如何在我登录(或启动)页面后自动加载领域数据?

0 个答案:

没有答案