Ios 更新后,我在UITableView的numberOfRowsInSection中得到了crash on return语句

Ios 更新后,我在UITableView的numberOfRowsInSection中得到了crash on return语句,ios,swift,uitableview,Ios,Swift,Uitableview,以下是我的代码,通过它,我可以从web服务中获取10条记录的数据: func callService() { ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId: UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(re

以下是我的代码,通过它,我可以从web服务中获取10条记录的数据:

func callService() {
    ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId:  UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
        SVProgressHUD.dismiss()
        self.totalPages = result.totalPages!
        let newItems = result.userdata?.newPostData

        tableView.beingUpdates()
        for item in newItems {
            self.listData.append(item)
            let indexPath = IndexPath(row:(self.listData!.count-1), section:0) // shouldn't this be just self.listData!.count as your adding a new row?
            tableView.insertRows(at: [indexPath], with: .left)
        }
        tableView.endUpdates()
    })
}
我得到的错误是:

由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第10行插入节0,但更新后节0中只有10行”

此崩溃发生在返回语句的numberOfRowsInSection上:

if listData != nil{
    print("list count in numberOfRowsInSection\(listData?.count)")
    return (listData?.count)!
}
return 0
问题是由于以下几行导致的,在这些行中,我试图从子视图控制器中的tableview内容更新scrollview高度:

if (UIApplication.visibleNavigationController.visibleViewController is UserPrfileControl){
            if (UIApplication.visibleNavigationController.visibleViewController as! UserPrfileControl).childControllerToUpdate == "userPost"{
                (UIApplication.visibleNavigationController.visibleViewController as! UserPrfileControl).commentViewHeight.constant = 0
                (UIApplication.visibleNavigationController.visibleViewController as! UserPrfileControl).burnedPostsHeight.constant = 0
                (UIApplication.visibleNavigationController.visibleViewController as! UserPrfileControl).userProfileViewHeight.constant = self.tableView.contentSize.height
            }
        }

我可以建议,当您第二次执行
callService()
时,您的表视图已经有10行,您可以尝试添加新记录。但是UITable view目前不调用
numberOfRowsInSection
,也不知道
listData
中的新项数。因此,您需要告诉UITable项目的数量已经更改,当您在
listData
中添加新项目时,只需调用
UITableView.reloadData()
函数即可

func callService() {
    ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId:  UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
        SVProgressHUD.dismiss()
        self.totalPages = result.totalPages!
        let newItems = result.userdata?.newPostData

        for item in newItems {
            self.listData.append(item)
        }

        tableView.reloadData()
    })
}

可能重复的请添加类型为
listData
;添加
numberOfRows
方法。至于你的评论“这不应该只是self.listData!.c”。。。否。它是正确的,因为您首先将数据附加到
listData
。因此,如果您从10开始,那么首先添加第11项,这意味着您将在第10个索引ergo中添加它
count-1==11-1==10
请粘贴您的实际代码-您有一个拼写错误
正在更新中
,加上您的
self.listData
numberOfRows
中是可选的,但不在第一个代码块中,并且您不打开
newItems
。所有这些错误都意味着您没有复制/粘贴代码。这里显示的代码是正确的,不会产生显示的错误。此外,您的
numberOfRows
可以减少到一行
return self.listData?count??0
谢谢大家,我所面临的问题是由于以下几行我试图根据子视图控制器中的tableview更新ScrollView高度:请指导我如何根据子视图控制器中tableview的内容大小更新ScrollView高度?