Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

swift - 快速访问tableFoRrowAtIndexpath外的tableview单元格期间发生致命错误(Fatal error during accesing tableview cell outside of cellFoRrowAtIndexpath in swift)

In my tableview i have 3 cells.

(在我的表格视图中,我有3个单元格。)

In my first cell, a pagerview is there to show number of photos.

(在我的第一个单元格中,有一个pagerview显示照片数量。)

when i am trying to access the cell out side of the cellFoRrowAtIndexpath, and reloading the pagerview there i am getting fatal error.I am getting the cell nil, when i am trying to access.

(当我尝试访问cellFoRrowAtIndexpath外部的单元格并重新加载pagerview时,出现致命错误。当我尝试访问时,单元格为零。)

My code is:-

(我的代码是:-)

        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()
            }

custom cell class for pagerview:-

(自定义页面类的单元格视图:)

        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
                            }
  ask by Tapan Raut translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...