Reputation: 76
I have a UITableView called myTableView. What I want the tableView to do is actually quite simple. If the user taps a cell it should expand, in other words an accordion to tableView. This part works fine for me, but I also want the Cell to expand when the user taps a marker on a mapView (I'm using Mapbox). The markers have titles which end with a number, I use this number to determine which cell should expand. Therefore I get the number from the title String and create an NSIndexPath (called localIndexPath) with the number as the row. After that I am selecting the according cell manually with:
self.myTableView.selectRowAtIndexPath(localIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
and after that I call didSelectRowAtIndexPath by:
self.tableView(self.myTableView, didSelectRowAtIndexPath: localIndexPath)
this then calls heightForRowAtIndexPath, which is called successfully both ways, but the actual height only changes when I click the cell, not when i click the marker. My question is why is that and how can I make it work both ways?
Heres the code of all the corresponding methods:
import UIKit
@IBDesignable class PullUpView: UIView, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableCell: UIView!
@IBOutlet weak var hideButton: UIButton!
@IBOutlet weak var myTableView: UITableView!
var view:UIView!
var selectedRowIndex = NSIndexPath(forRow: 99, inSection: 0)
var localIndexPath = NSIndexPath(forRow: 99, inSection: 0)
//irrelevant code...
//This is the function I call when I click a marker
func callDidSelectRowAtIndexPathManually(clickedAnnotation: String){
print("MANUALLY: \(clickedAnnotation)")
var tourNumber: String = "Default"
if(clickedAnnotation.characters.count == 18){
let idx = advance(clickedAnnotation.startIndex, 15)
tourNumber = String(clickedAnnotation[idx])
// print("TOUR NUMMER IST: \(tourNumber)")
//in case of two digit number
}else if(clickedAnnotation.characters.count == 19){
let idx = advance(clickedAnnotation.startIndex, 15)
let idxTwo = advance(clickedAnnotation.startIndex, 16)
tourNumber = String(clickedAnnotation[idx]) + String(clickedAnnotation[idxTwo])
// print("TOUR NUMMER IST: \(tourNumber)")
}
let tourNumberAsInt = Int(tourNumber)
print("TOUR NUMMER IST: \(tourNumberAsInt)")
if(tourNumberAsInt != nil){
localIndexPath = NSIndexPath(forRow: tourNumberAsInt!, inSection: 0)
//The manual selection and call
self.myTableView.selectRowAtIndexPath(localIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
self.tableView(self.myTableView, didSelectRowAtIndexPath: localIndexPath)
getCellHeight(localIndexPath)
}
}
//I just store the indexPath in a variable
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
NSLog("Und gewählter Index ist: \(indexPath.row)")
selectedRowIndex = indexPath
myTableView.beginUpdates()
print("did start updates")
myTableView.endUpdates()
print("did end updates")
getCellHeight(selectedRowIndex)
}
//I use this function to check the height of the cell
func getCellHeight(indexPath: NSIndexPath){
var cell = tableView(myTableView, cellForRowAtIndexPath: indexPath)
print("HÖHE VON: \(indexPath.row) ist jetzt: \(cell.bounds.height)")
}
//I check wheter selectedIndexPath or localIndexPath are equal to the indexPath argument, if it is the cell should expand
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(selectedRowIndex == indexPath && selectedRowIndex.row == indexPath.row || localIndexPath == indexPath){
print("heightforrow CALLED für: \(indexPath.row)")
return 200
}
print("heightforrow NOT CALLED für: \(indexPath.row)")
return 60
}
Sorry for the prints in german, but I think you can make up what they mean(ist = is, für = for, höhe von = height of).
And here's the log when I tap the second cell, this is working fine, cell expands as I intended, even tho the heigh reads 44:
did start Updates
heightforrow NOT CALLED für: 0
heightforrow CALLED für: 1
heightforrow NOT CALLED für: 2
did end updates
HÖHE VON: 1 ist jetzt: 44.0
And this is the log when i tap an annotation, the cell isn't expanding visually:
did start updates
heightforrow NOT CALLED für: 0
heightforrow NOT CALLED für: 1
heightforrow CALLED für: 2
heightforrow NOT CALLED für: 0
heightforrow NOT CALLED für: 1
heightforrow CALLED für: 2
did end updates
HÖHE VON: 2 ist jetzt: 44.0
HÖHE VON: 2 ist jetzt: 44.0
Thanks in advance and sorry for my bad english!
Upvotes: 2
Views: 849
Reputation: 76
Thanks a lot to everybody who tried to help me with this problem!!! I tried everything but I wasn't able to make it work. While Researching I found an approach that works way better for what I wanted than my code posted here. Here's what I'm doing now: https://www.youtube.com/watch?v=VWgr_wNtGPM with the help of: Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer - ios
Upvotes: 1
Reputation: 41
You have to write a function like below:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44.00
}
Upvotes: 0