I have an app with about 30 active users and 500+ registered users and this issue is only happening with one person who is on 9.2 using an iPhone 6.
I can see from crash
there is still a little bit of inconsistency in your code. try to distinguish between self.comentsDetailed
and comentsDetailed
in you code. try to choose another name for the local value let cd = self.commentsDetailed![indexPath.row-3]
for an example, and next use this value where it is necessary in next statements. this can help you to see what wrong happens in your code.
I think the only possible point where the second else can fail is the commentsDetailed.comment!
unwrapping, which should fail in case it were equal to nil. The rest seems perfectly robust, including the heightForView
method. If checking commentsDetailed.comment != nil
doesn't do the trick, I'll start thinking it's some random 9.2 early bug...
The error is array out of bounce at the last else closure like you said:
if self.commentsDetailed != nil && self.commentsDetailed!.count >= (indexPath.row - 3)
{
let commentsDetailed = self.commentsDetailed![indexPath.row-3]
If for example the array is size 1, then the last element you can index is arr[0] but count >= (indexPath.row - 3) will let indexing arr[1] which will cause the error.
There are also other fixes you can do to optimize since this method is called repeatedly. (I'd suggest maybe having the fonts and other variables outside so that it doesn't create them each time. There maybe such optimization behind but I'm not sure).
I had to re-write the whole thing playground to find the problem so I guess I'll just paste the code here :)
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
}
}