2016-03-07 23 views
1

我有一个单元,其中有一个从Amazon S3中检索的图像。此图像的高度会影响单元格的高度,但不幸的是,在确定单元格高度后,将在完成处理程序中检索图像。如果我向下滚动并正确地重新加载单元格。由于完成处理程序未正确计算单元格高度

之前滚动(不正确的单元格的高度): enter image description here

小区配置(删节为可读性一些不必要的东西)::

func setCompletionCell(completion: CompletionPublic, contentType: String, classType: String){ 
     self.completionPub = completion 
     self.selectionStyle = UITableViewCellSelectionStyle.None //Disables clicking 

     //Setting image or video 
     if (contentType == "image"){ 
      nwa.fetchSignedUrl("image/accepted/" + completion.mediaId + ".png") { (result, err) in 
       self.nwa.fetchImage(result) { (image, err) in 

        if image != nil{ 
         let screenSize: CGRect = UIScreen.mainScreen().bounds 

         var multiplyNum = screenSize.width/image.size.width 
         //if image height is going to be more than 60% of the screen, resize width and height to ensure that it isn't greater than 60% while keeping the aspect ratio correct 
         if ((image.size.height*multiplyNum) > (screenSize.height*0.6)){ 
          multiplyNum = screenSize.height*0.6/image.size.height 
          self.imageViewWidthConstraint.constant = (multiplyNum*image.size.width) 
          self.imageViewHeightConstraint.constant = screenSize.height*0.6 
         } 
         else{ 
          self.imageViewWidthConstraint.constant = screenSize.width 
          self.imageViewHeightConstraint.constant = (multiplyNum*image.size.height) 
         } 
         self.imgView.image = image 

        } 
        else{ 
         //no image returned 
        } 


       } 
      } 
     } 
     else if (contentType == "video"){ 
      ytplayer.loadWithVideoId(completion.mediaId) 
     } 


    } 

enter image description here

滚动(正确的单元格高度)后TableView代理方法:

func callNWT(tableView: UITableView, completionHandler:() ->()) { 
    switch trendingToggle { 
    case 0: 
     nwt.getTrendingBounties(0) { (bountyArr, err) in 
      //@TODO: change pos 
      if bountyArr == nil { 
       self.bountyArr = [] 
      } 
      else { 
       self.bountyArr = bountyArr as [BountyPublic] 
      } 

      if self.bountyArr.count == 0 { 
       completionHandler() 
      } 
      self.reloadTableViewContent(tableView) 
     } 
    case 1: 
     nwt.getTrendingCompletions(0) { (compArr, err) in 
      if compArr == nil { 
       self.compArr = [] 
      } 
      else { 
       self.compArr = compArr as [CompletionPublic] 
      } 
      if self.compArr.count == 0 { 
       completionHandler() 
      } 
      self.reloadTableViewContent(tableView) 
     } 
    case 2: 
     nwt.getTrendingPeople(0) { (peopleArr, err) in 
      if peopleArr == nil { 
       self.peopleArr = [] 
      } 
      else { 
       self.peopleArr = peopleArr as [Person] 
      } 

      if self.peopleArr.count == 0 { 
       completionHandler() 
      } 
      self.reloadTableViewContent(tableView) 
     } 
    default: 
     break 
    } 
} 

func configureTableView(tableView: UITableView){ 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 500.0 
    tableView.allowsSelection = false; //disables selection highlighting of cells 
    tableView.tableFooterView = UIView() 
    tableView.dataSource = self 
} 

func reloadTableViewContent(tableView: UITableView) { 
    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     tableView.reloadData() 
     print("reloading table view content") 
     tableView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: false) 
    }) 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if trendingToggle == 0 { 
     return bountyArr.count 
    } 
    else if trendingToggle == 1 { 
     return compArr.count 
    } 
    else { 
     return peopleArr.count 
    } 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if trendingToggle == 0 { 
     return bountyCellAtIndexPath(tableView, indexPath: indexPath) 
    } 
    else if trendingToggle == 1 { 
     return completedCellAtIndexPath(tableView, indexPath:indexPath) 
    } 
    else { 
     return personCellAtIndexPath(tableView, indexPath: indexPath) 
    } 
} 

func completedCellAtIndexPath(tableView: UITableView, indexPath:NSIndexPath) -> CompletedCell{ 
    var cell: CompletedCell 
    if compArr[indexPath.row].contentType == "image" { 
     cell = tableView.dequeueReusableCellWithIdentifier(completedImgCellIdentifier) as! CompletedCell 
     let comp = compArr[indexPath.row] 
     cell.setCompletionCell(comp, contentType: "image", classType: "trending") 
    } 
    else { //video 
     cell = tableView.dequeueReusableCellWithIdentifier(completedVidCellIdentifier) as! CompletedCell 
     let comp = compArr[indexPath.row] 
     cell.setCompletionCell(comp, contentType: "video", classType: "trending") 
    } 
    return cell 
} 

如何确保第一次正确计算细胞高度?有没有一种方法可以延迟代码执行直到检索到图像?或者这不是一个好主意?

+1

发表一些代码,你的表查看委托方法和单元配置。 – Lucho

+0

您绝不应该暂停网络请求的主队列。你应该做的是设置图像后,你必须通知表视图布局子视图。 –

+0

你可以在这里找到这个示例代码 http://stackoverflow.com/questions/33975355/updating-cell-height-after-image-downloads – AdamM

回答

1

在完成处理程序中重新载入tableview。

tableView.reloadData()

在用于数据源/委托协议heightForRowAtIndex方法重新计算使用图像的高度和对于每个单独的小区返回适当的值。

当您调用reloadData()时,再次调用所有数据源/委托方法,因此返回正确的高度将允许您根据需要调整单元的大小。

+0

重新加载桌面视图不会工作,除非图像在本地保存和检索。此外,您可以重新加载该单元格,而不是一次又一次地重新加载整个表格。 – Nishant

+0

你是正确的整个tableView不需要重新加载 - 只是有问题的行,但为什么图像需要保存在本地? (我假定你的意思是在应用程序包中)。为什么你不能从服务器存储它的数据库中的图像,并调用你喜欢的reloadTableView方法,然后在heightForRowAtIndex方法中返回对应于indexPath.row的图像的高度图像数组? –

+0

通过在本地保存然后调用tableView.reloadData()而不是从S3再次获取图像来解决。谢谢@Nishant和布莱克。 – Mitchell

相关问题