Tak więc wygląda na to, że heightForRowAtIndexPath
jest wywoływane przed cellForRowAtIndexPath
. Obecnie używam cellForRowAtIndexPath
do obliczenia wysokości każdej komórki (są one zmienne wysokości, używając UILabel
z różnymi ilościami tekstu w nim.Wysokość wywołaniaForRowAtIndexPath z ciągu cellForRowAtIndexPath?
Jestem zaintrygowany, jak dokładnie mogę ustawić wysokość komórki poprawnie jeżeli metoda zestawy wysokość nazywa się przed metody, która oblicza to.
Utworzyłem kategorię NSString
, a drugi z UILabel
, które współpracują ze sobą zrobić UILabel
odpowiedniej wielkości. Kwestia jest to, że instancje UITableViewCell
zawierające te etykiety nie są zmieniane, więc etykiety zwisają nad końcem każdej etykiety, zachodząc na te znajdujące się poniżej.
Wiem, że muszę zrobić, aby wysokość komórki była równa wysokości etykiety, plus dodatkowe pole marginesowe, ale jestem zdziwiony, w jaki sposób mogę ustawić wysokość przed wysokością etykiety zostały obliczone. Czy mogę ustawić wysokość kogoś bez użycia heightForRowAtIndexPath
? A jeśli nie, czy mogę obliczyć wysokość komórki w innym miejscu? Obecnie używam indexPath.row
, aby tak się stało, więc zrobienie tego z góry byłoby trudne. Oto kod mojego cellForRowAtIndexPath:
cellForRowAtIndexPath
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
if (_customCell == nil) {
_customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
}
_customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
_customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];
UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
backView.backgroundColor = [UIColor clearColor];
_customCell.backgroundView = backView;
_customCell.myLabel.frame = CGRectMake(5, 5, 265, 65);
[_customCell.myLabel sizeToFitMultipleLines];
return _customCell;
}
Począwszy od wersji iOS 8, można również użyć automatycznie obliczanych dynamicznych wysokości rzędów. http: // stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Suragch