在swift中访问cellFoRrowAtIndexpath之外的tableview单元格时发生致命错误

在swift中访问cellFoRrowAtIndexpath之外的tableview单元格时发生致命错误,swift,tableview,Swift,Tableview,在我的tableview中,我有3个单元格。在我的第一个手机里,有一个pagerview可以显示照片的数量。当我试图访问cellFoRrowAtIndexpath外侧的单元格,并在那里重新加载pagerview时,我遇到了致命错误。当我试图访问时,我得到的单元格为零。 我的代码是:- class UserDetailTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

在我的tableview中,我有3个单元格。在我的第一个手机里,有一个pagerview可以显示照片的数量。当我试图访问cellFoRrowAtIndexpath外侧的单元格,并在那里重新加载pagerview时,我遇到了致命错误。当我试图访问时,我得到的单元格为零。

我的代码是:-

        class UserDetailTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

        override func viewDidLoad() {
            super.viewDidLoad()
            setDataSourceAndDelegate()
            registerCustomCell()

             getUserDetailsData(uid as! Int, accessToken: accessToken)
            }

             func registerCustomCell() {
             tableView.register(UINib(nibName: "MatchDetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "MatchCell")
                   tableView.register(UINib(nibName: "UserDetailTableviewCell", bundle: nil), forCellReuseIdentifier: "UserDetailTableviewCell")
                   tableView.register(UINib(nibName: "pagerTableViewCell", bundle: nil), forCellReuseIdentifier: "pagerTableViewCell")
            // Register PageView Cell
           self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        }
        func setDataSourceAndDelegate() {
            tableView.delegate = self
            tableView.dataSource = self

        }

         func getUserDetailsData(_ userId: Int, accessToken: String) {
             let indexPath = IndexPath(row: 0, section: 0)
                              let cell = self.tableView.cellForRow(at: indexPath) as? pagerTableViewCell
            apiRequest.getUserDetailData(userId, accessToken) { (userDetails) in

                self.userDataResult = userDetails?.result


                cell?.modelMoreImageArray = (self.userDataResult?.imagelist)!

               cell!.setCollectionViewDataSourceDelegate()
            }
            }
             func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch section {
            case 0:
                return 1
            case 1:
                return 1
            case 2:
                return 8
            default:
                return 0
            }

        }
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            switch indexPath.section {
            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "pagerTableViewCell", for: indexPath) as? pagerTableViewCell

                                    return cell!
            case 1:
                     let cell = tableView.dequeueReusableCell(withIdentifier: "UserDetailTableviewCell", for: indexPath) as? UserDetailTableviewCell

                       cell?.setUpCell(details: userDataResult!)
                            return cell!
                            }
                            default:
                return UITableViewCell()
            }
        class pagerTableViewCell: UITableViewCell,FSPagerViewDelegate,FSPagerViewDataSource {

        @IBOutlet weak var pagerView: FSPagerView!
        var modelMoreImageArray = [Imagelist]()


        override func awakeFromNib() {
            super.awakeFromNib()
            self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")

        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

        func setCollectionViewDataSourceDelegate() {
        print(modelMoreImageArray)
           pagerView.dataSource = self

           pagerView.delegate = self
            pagerView.reloadData()
        }

         }

          public func numberOfItems(in pagerView: FSPagerView) -> Int {
               print(modelMoreImageArray.count)
               if modelMoreImageArray.count > 0{
                print(modelMoreImageArray.count)
                   return modelMoreImageArray.count
               }
               return 0

           }

                public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
               let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)


              cell.imageView?.image = UIImage(named: "bg0")
            for imagedata in modelMoreImageArray {
                let imageUrl = BASE_URL + imagedata.imagePath
                          //set activity indicator until image is loaded
                          cell.imageView?.contentMode = .scaleAspectFill
                          cell.imageView?.kf.indicatorType = .activity
                          if let url = URL(string: imageUrl) {
                              let resource = ImageResource(downloadURL:  url)
                              cell.imageView?.kf.setImage(with: resource)
                          }
            }


                            return cell
                            }
页面视图的自定义单元格类:-

        class UserDetailTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

        override func viewDidLoad() {
            super.viewDidLoad()
            setDataSourceAndDelegate()
            registerCustomCell()

             getUserDetailsData(uid as! Int, accessToken: accessToken)
            }

             func registerCustomCell() {
             tableView.register(UINib(nibName: "MatchDetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "MatchCell")
                   tableView.register(UINib(nibName: "UserDetailTableviewCell", bundle: nil), forCellReuseIdentifier: "UserDetailTableviewCell")
                   tableView.register(UINib(nibName: "pagerTableViewCell", bundle: nil), forCellReuseIdentifier: "pagerTableViewCell")
            // Register PageView Cell
           self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        }
        func setDataSourceAndDelegate() {
            tableView.delegate = self
            tableView.dataSource = self

        }

         func getUserDetailsData(_ userId: Int, accessToken: String) {
             let indexPath = IndexPath(row: 0, section: 0)
                              let cell = self.tableView.cellForRow(at: indexPath) as? pagerTableViewCell
            apiRequest.getUserDetailData(userId, accessToken) { (userDetails) in

                self.userDataResult = userDetails?.result


                cell?.modelMoreImageArray = (self.userDataResult?.imagelist)!

               cell!.setCollectionViewDataSourceDelegate()
            }
            }
             func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch section {
            case 0:
                return 1
            case 1:
                return 1
            case 2:
                return 8
            default:
                return 0
            }

        }
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            switch indexPath.section {
            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: "pagerTableViewCell", for: indexPath) as? pagerTableViewCell

                                    return cell!
            case 1:
                     let cell = tableView.dequeueReusableCell(withIdentifier: "UserDetailTableviewCell", for: indexPath) as? UserDetailTableviewCell

                       cell?.setUpCell(details: userDataResult!)
                            return cell!
                            }
                            default:
                return UITableViewCell()
            }
        class pagerTableViewCell: UITableViewCell,FSPagerViewDelegate,FSPagerViewDataSource {

        @IBOutlet weak var pagerView: FSPagerView!
        var modelMoreImageArray = [Imagelist]()


        override func awakeFromNib() {
            super.awakeFromNib()
            self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")

        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

        func setCollectionViewDataSourceDelegate() {
        print(modelMoreImageArray)
           pagerView.dataSource = self

           pagerView.delegate = self
            pagerView.reloadData()
        }

         }

          public func numberOfItems(in pagerView: FSPagerView) -> Int {
               print(modelMoreImageArray.count)
               if modelMoreImageArray.count > 0{
                print(modelMoreImageArray.count)
                   return modelMoreImageArray.count
               }
               return 0

           }

                public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
               let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)


              cell.imageView?.image = UIImage(named: "bg0")
            for imagedata in modelMoreImageArray {
                let imageUrl = BASE_URL + imagedata.imagePath
                          //set activity indicator until image is loaded
                          cell.imageView?.contentMode = .scaleAspectFill
                          cell.imageView?.kf.indicatorType = .activity
                          if let url = URL(string: imageUrl) {
                              let resource = ImageResource(downloadURL:  url)
                              cell.imageView?.kf.setImage(with: resource)
                          }
            }


                            return cell
                            }