2015-12-15 48 views
0

我有大约30活跃用户和500多个注册用户的应用程序,这个问题只有谁是9.2使用的是iPhone 6heightForRowAtIndexPath崩溃间歇

我可以从崩溃报告中看到她有一人发生超过60次崩溃,她表示问题有时可以,而不是其他问题。

我在这里有相同的测试设备,没有问题发生。

我不能从织物中得到太多的崩溃报告细节,但崩溃发生在以下方法和应用程序正在崩溃在这个方法的最后其他,我可以从她的描述问题:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
    { 
     let bounds = UIScreen.mainScreen().bounds 
     var screenWidth = bounds.size.width 
     let screenHeight = bounds.size.height 
     if indexPath.row == 0 { 
      let font = UIFont(name: "Helvetica Neue", size: 17.0) 
      let heightCalc = heightForView(self.postDescription!, font: font!, width: screenWidth - 32) 
      if self.photoWidth != nil{ 

       let imageAspectRatio = self.photoWidth!/self.photoHeight! 
       var imageViewFrameHeight = screenWidth/imageAspectRatio 
       if self.portBool { 
        if imageViewFrameHeight > (screenHeight - 64) { 
         screenWidth = screenWidth * 0.60 
         imageViewFrameHeight = imageViewFrameHeight * 0.60 
        } 
       } 

       return imageViewFrameHeight + 136 + heightCalc + 26 
       //return 557 
      } 
      else{ 
       return 136 + heightCalc + 30 
      } 
     } 
     else if indexPath.row == 1 { 
      return 50.0 
     } 
     else if indexPath.row == 2{ 
      return 40.0 
     } 
     else{ 

      if self.commentsDetailed != nil && self.commentsDetailed!.count >= (indexPath.row - 3) 
      { 
       let commentsDetailed = self.commentsDetailed![indexPath.row-3] 
       let font = UIFont(name: "Helvetica Neue", size: 14.0) 

       let heightCalc = heightForView(commentsDetailed.comment!, font: font!, width: screenWidth - 51) 

       if commentsDetailed.authorId == Prefs.userId.description { 
        return 45 + heightCalc + 40 + 30 
        //return 131.5 
       } 
       else{ 
        return 45 + heightCalc + 40 
        //return 101.5 
       } 

      } 
      else { 
       return 80.0 
      } 

     } 
    } 

对于那些想知道,我需要计算的高度,以避免将其当我使用表视图自动尺寸发生抖动的一个滚动。所以我只需要在这里解决问题而不是切换方法。

从crashlytics一些信息:

EXC_BREAKPOINT 

Thread : Crashed: Thread 
0 ProjectName     0x10008a64c specialized NewFeedDetailedController.tableView(UITableView, heightForRowAtIndexPath : NSIndexPath) -> CGFloat (NewFeedDetailedController.swift) 
1 ProjectName     0x100085258 @objc NewFeedDetailedController.tableView(UITableView, heightForRowAtIndexPath : NSIndexPath) -> CGFloat (NewFeedDetailedController.swift) 
2 UIKit       0x187683638 __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 396 
3 UIKit       0x187641818 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 3948 
4 UIKit       0x1876407d4 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 412 
5 UIKit       0x187640590 -[UITableViewRowData heightForTable] + 64 
6 UIKit       0x18764039c -[UITableView _updateContentSize] + 220 
7 UIKit       0x1878a6f60 -[UITableView _rebuildGeometry] + 44 
8 UIKit       0x1876462c8 -[UITableView didMoveToWindow] + 144 
9 UIKit       0x18755705c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1496 
10 UIKit       0x18757c568 -[UIScrollView _didMoveFromWindow:toWindow:] + 92 
11 UIKit       0x187556d7c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 760 
12 UIKit       0x187556310 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 152 
13 Foundation      0x183189500 -[NSISEngine withBehaviors:performModifications:] + 168 
14 UIKit       0x187556194 -[UIView(Hierarchy) _postMovedFromSuperview:] + 532 
15 UIKit       0x187563b80 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1784 
16 UIKit       0x187755600 -[_UIParallaxDimmingView didMoveToWindow] + 180 
17 UIKit       0x18755705c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1496 
18 UIKit       0x187556d7c -[UIView(Internal) _didMoveFromWindow:toWindow:] + 760 
19 UIKit       0x187556310 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 152 
20 Foundation      0x183189500 -[NSISEngine withBehaviors:performModifications:] + 168 
21 UIKit       0x187556194 -[UIView(Hierarchy) _postMovedFromSuperview:] + 532 
22 UIKit       0x187563b80 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1784 
23 UIKit       0x1877f5db4 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke_2 + 1656 
24 UIKit       0x18756a964 +[UIView(Animation) performWithoutAnimation:] + 80 
25 UIKit       0x187755118 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 260 
26 UIKit       0x187870840 +[UIView(Internal) _performBlockDelayingTriggeringResponderEvents:] + 220 
27 UIKit       0x187754c90 -[_UINavigationParallaxTransition animateTransition:] + 1060 
28 UIKit       0x18770e6a0 -[UINavigationController _startCustomTransition:] + 3544 
29 UIKit       0x18761a9b8 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688 
30 UIKit       0x18761a694 -[UINavigationController __viewWillLayoutSubviews] + 60 
31 UIKit       0x18761a5fc -[UILayoutContainerView layoutSubviews] + 208 
32 UIKit       0x187557778 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 656 
33 QuartzCore      0x184f66b2c -[CALayer layoutSublayers] + 148 
34 QuartzCore      0x184f61738 CA::Layer::layout_if_needed(CA::Transaction*) + 292 
35 QuartzCore      0x184f615f8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32 
36 QuartzCore      0x184f60c94 CA::Context::commit_transaction(CA::Transaction*) + 252 
37 QuartzCore      0x184f609dc CA::Transaction::commit() + 512 
38 UIKit       0x18754dc78 _afterCACommitHandler + 180 
39 CoreFoundation     0x182820588 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32 
40 CoreFoundation     0x18281e32c __CFRunLoopDoObservers + 372 
41 CoreFoundation     0x18281e75c __CFRunLoopRun + 928 
42 CoreFoundation     0x18274d680 CFRunLoopRunSpecific + 384 
43 GraphicsServices    0x183c5c088 GSEventRunModal + 180 
44 UIKit       0x1875c4d90 UIApplicationMain + 204 
45 ProjectName     0x1001663b0 main (AppDelegate.swift:21) 
46 libdyld.dylib     0x1822ee8b8 start + 4 

而且heightForView FUNC:

func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{ 
     let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max)) 
     label.numberOfLines = 0 
     label.lineBreakMode = NSLineBreakMode.ByWordWrapping 
     label.font = font 
     label.text = text 

     label.sizeToFit() 
     return label.frame.height 
    } 

更新的代码与检查零,用户仍然会崩溃:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
    { 
     let bounds = UIScreen.mainScreen().bounds 
     var screenWidth = bounds.size.width 
     let screenHeight = bounds.size.height 
     if indexPath.row == 0 { 
      CLSLogv("crashing in indexPath.Row = 0 %@", getVaList(["returning 50.0 here"])) 

      let font = UIFont(name: "Helvetica Neue", size: 17.0) 

      if let description = self.postDescription { 
       let heightCalc = heightForView(description, font: font!, width: screenWidth - 32) 
       if self.photoWidth != nil && self.photoHeight != nil { 
        CLSLogv("crashing in indexPath.Row = 0 %@", getVaList(["inside photowidth is not nil"])) 
        let imageAspectRatio = self.photoWidth!/self.photoHeight! 
        var imageViewFrameHeight = screenWidth/imageAspectRatio 
        if self.portBool { 
         if imageViewFrameHeight > (screenHeight - 64) { 
          screenWidth = screenWidth * 0.60 
          imageViewFrameHeight = imageViewFrameHeight * 0.60 
         } 
        } 

        return imageViewFrameHeight + 136.0 + heightCalc + 26.0 
        //return 557 
       } 
       else{ 
        CLSLogv("crashing in indexPath.Row = 0 %@", getVaList(["when photowidth is nil"])) 
        return 136.0 + heightCalc + 30.0 
       } 
      } 
      else{ 
       CLSLogv("crashing in indexPath.Row = 0 %@", getVaList(["unable to unwrap self.postDescription"])) 
       if self.photoWidth != nil && self.photoHeight != nil { 
        let imageAspectRatio = self.photoWidth!/self.photoHeight! 
        var imageViewFrameHeight = screenWidth/imageAspectRatio 
        if self.portBool { 
         if imageViewFrameHeight > (screenHeight - 64) { 
          screenWidth = screenWidth * 0.60 
          imageViewFrameHeight = imageViewFrameHeight * 0.60 
         } 
        } 

        return imageViewFrameHeight + 136.0 + 26.0 
       } 
       else{ 
        return 136.0 + 30.0 
       } 
      } 
     } 
     else if indexPath.row == 1 { 
      CLSLogv("crashing in indexPath.Row = 1 %@", getVaList(["returning 50.0 here"])) 
      return 50.0 
     } 
     else if indexPath.row == 2 { 
      CLSLogv("crashing in indexPath.Row = 2 %@", getVaList(["returning 40.0 here"])) 
      return 40.0 
     } 
     else{ 

      if self.commentsDetailed != nil && self.commentsDetailed!.count > (indexPath.row - 3) 
      { 

       if let commentsDetailed = self.commentsDetailed?[indexPath.row-3]{ 


        let font = UIFont(name: "Helvetica Neue", size: 14.0) 
        if let comment = commentsDetailed.comment{ 
         let heightCalc = heightForView(comment, font: font!, width: screenWidth - 51) 
         if commentsDetailed.authorId == Prefs.userId.description { 
          return 45.0 + heightCalc + 40.0 + 30.0 
         } 
         else{ 
          return 45.0 + heightCalc + 40.0 
         } 
        } 
        else{ 
         if commentsDetailed.authorId == Prefs.userId.description { 
          return 45.0 + 40.0 + 30.0 
         } 
         else{ 
          return 45.0 + 40.0 
         } 
        } 
       } 
       else { 
        CLSLogv("in the else for defining commentsDetailed %a",getVaList(["commentsdetailed"])) 
        return 80.0 
       } 
      } 
      else { 
       CLSLogv("in the condition for checking commentsDetailed = nil - true %a", getVaList(["CRASH RELATED TO RETURNING 80 IF ARRAY IS NIL?"])) 
       return 80.0 
      } 

     } 
    } 
+1

有没有例外类型,你确定commentsDetailed.comment不能为零? –

+0

@NaotoIda在崩溃报告的顶部它说:EXC_BREAKPOINT – user2363025

+0

@NaotoIda我敢肯定commentsDetailed.comment不能为零。我自己做了网络服务 – user2363025

回答

1

我认为第二个人可能会失败的唯一可能点是commentsDetailed.comment!解开,如果它等于零,应该失败。其余的看起来非常健壮,包括heightForView方法。如果检查commentsDetailed.comment != nil不会做的伎俩,我就开始想它的一些随机早期的9.2 ...错误

+0

我知道commentsDetailed.comment永远不会是零,因为我已经将其纳入查询 – user2363025

+0

其几乎不需要使用'return 45.0 + heightCalc + 40.0 + 30.0'而不是'返回45 + heightCalc + 40 + 30',这是我能想到的唯一的事情,但肯定如果这是一个问题,它不会间歇性地发生,它不会特定于一个用户? – user2363025

+0

这就是为什么我怀疑它是数据相关或与sdk有关。我还没有使用9.2版本,但如果它最终成为一个操作系统的错误,它绝对不会是第一次... – Bartserk

1

的错误是阵列出反弹在最后关闭其他像你说:

if self.commentsDetailed != nil && self.commentsDetailed!.count >= (indexPath.row - 3) 
    { 
     let commentsDetailed = self.commentsDetailed![indexPath.row-3] 

例如,如果数组的大小为1,那么您可以索引的最后一个元素是arr [0],但是count> =(indexPath.row - 3)会让索引arr [1],从而导致错误。

还有其他修复方法可以用来优化,因为此方法被重复调用。 (我建议可能有外部的字体和其他变量,这样它就不会每次都创建它们,可能有这样的优化,但我不确定)。

我不得不重新写了整个事情的操场,所以我想我就贴在这里的代码:)

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: 
    NSIndexPath) -> CGFloat { 
    var screenWidth = UIScreen.mainScreen().bounds.size.width 
    let screenHeight = UIScreen.mainScreen().bounds.size.height 

    switch indexPath.row { 

    case 0: 

     guard let font = UIFont(name: "Helvetica Neue", size: 17.0) else { 
      print("Font not Found !") 
      break 
     } 
     let heightCalc = heightForView(self.postDescription! ?? "", font: font, width: screenWidth - 32) 

     guard let photoWidth = self.photoWidth, let photoHeight = self.photoHeight else { 
      return 136 + heightCalc + 30 
     } 
     let imageAspectRatio = photoWidth/photoHeight 
     var imageViewFrameHeight = screenWidth/imageAspectRatio 
     if self.portBool { 
      if imageViewFrameHeight > (screenHeight - 64) { 
       screenWidth = screenWidth * 0.60 
       imageViewFrameHeight = imageViewFrameHeight * 0.60 
      } 
     } 
     return imageViewFrameHeight + 136 + heightCalc + 26 
     //return 557 

    case 1: return 50.0 
    case 2: return 40.0 

    case _ where self.commentsDetailed != nil && indexPath.row >= 3: 

     let index = indexPath.row - 3 
     //I think your problem was here (your index can not be equal to the count!) 
     guard let detailed = self.commentsDetailed where detailed.count > index 
      else { break } 

     let commentsDetailed = detailed[index] 
     guard let font = UIFont(name: "Helvetica Neue", size: 14.0) else { 
      break //font not found 
     } 

     let heightCalc = heightForView(commentsDetailed.comment! ?? "", font: font, width: screenWidth - 51) 

     if commentsDetailed.authorId == Prefs.userId.description { //make sure author id is always string. sounds like a number 
      return 45 + heightCalc + 40 + 30 
      //return 131.5 
     } 
     else{ 
      return 45 + heightCalc + 40 
      //return 101.5 
     } 

    default: return 80.0 
    } 
} 
+0

检查indexPath.row - 3是否> = 0的原因是因为前3个单元格不是来自阵列 – user2363025

+0

好吧,但那是唯一的地方代码可能会破坏:let commentsDetailed = self.commentsDetailed![indexPath.row-3]。我把它放在里面: if self.commentsDetailed!count>(indexPath.row - 3){} 否则,那里是一个机会,你可能会访问self.commentsDetailed![self.commentsDetailed!.count] //这是非法的 – Lukas

+0

感谢您的回应,但如果问题确实是一个数组越界,这将一直发生,并不是特定于一用户还是我错过了什么? – user2363025

0

仍有不一致的在你的代码一点点地发现问题。尝试在您的代码中区分self.comentsDetailedcomentsDetailed。尝试为本地值let cd = self.commentsDetailed![indexPath.row-3]选择另一个名称作为示例,然后在下一个语句中需要时使用此值。这可以帮助您查看代码中发生了什么错误。

相关问题