myTableView という UITableView があります。tableView で実行したいことは、実際には非常に単純です。ユーザーがセルをタップすると、セルが展開されます。つまり、tableView へのアコーディオンです。この部分は私にとってはうまく機能しますが、ユーザーが mapView のマーカーをタップしたときにセルを展開したい (私は Mapbox を使用しています)。マーカーには数字で終わるタイトルがあり、この数字を使用してどのセルを展開するかを決定します。したがって、タイトル文字列から番号を取得し、その番号を行として NSIndexPath (localIndexPath と呼ばれる) を作成します。その後、次の方法で手動で適切なセルを選択しています。
self.myTableView.selectRowAtIndexPath(localIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
その後、次の方法で didSelectRowAtIndexPath を呼び出します。
self.tableView(self.myTableView, didSelectRowAtIndexPath: localIndexPath)
これは、両方の方法で正常に呼び出される heightForRowAtIndexPath を呼び出しますが、実際の高さは、マーカーをクリックしたときではなく、セルをクリックしたときにのみ変化します。私の質問は、それがなぜであり、どうすれば両方の方法で機能させることができるのですか?
対応するすべてのメソッドのコードは次のとおりです。
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
}
ドイツ語のプリントで申し訳ありませんが、意味を理解できると思います (ist = is、für = for、höhe von = 高さ)。
そして、これが2番目のセルをタップしたときのログです。これは正常に機能しており、セルは意図したとおりに拡張されます。
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
そして、これは注釈をタップしたときのログです。セルは視覚的に拡大していません:
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
事前に感謝し、私の下手な英語で申し訳ありません!