У меня есть UITableView, заполненный пользовательскими UITableViewCells.В этих пользовательских ячейках у меня есть UITextField и кнопка «See More».Цель UIButton - динамически расширять этот конкретный UITableCell, когда пользователь хочет прочитать больше текста.Таким же образом, когда пользователь желает вернуться к исходному размеру, он снова нажимает кнопку, и UITableViewCell сжимается до исходного размера.
Поскольку ячейка не выбрана, я настраиваюIBAction в пользовательской ячейке, такой как:
// В CustomCell.m
- (IBAction)showMoreText:(id)sender
{
//instance bool variable to flag whether the cell has been resized
self.hasBeenResized = YES;
//turn off mask to bounds, otherwise cell doesnt seem to resize
[[self.cellView layer] setMasksToBounds:NO];
// Calculate the new sizes and positions for the textView and the button
CGRect newTextViewFrame = self.textView.frame;
newTextViewFrame.size.height = self.textView.contentSize.height;
self.textView.frame = newTextViewFrame;
CGFloat bottomYPos = self.textView.frame.origin.y + self.textView.frame.size.height;
CGRect buttonFrame = self.showMoreButton.frame;
buttonFrame.origin.y = bottomYPos;
self.showMoreButton.frame = buttonFrame;
// Call begin and end updates
[(UITableView*) self.superview beginUpdates];
[(UITableView*) self.superview endUpdates];
// Set mask and put rounded corners on the cell
[[self.cellView layer] setMasksToBounds:YES];
[[self.cellView layer] setCornerRadius:10.0];
}
После этого у меня есть это в моем классе ViewController:
// Within ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"heightForRowAtIndexPath");
CustomCell *cell = (CustomCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
if([cell hasBeenResized] == NO)
{
return cell.frame.size.height + 20;
}
else
{
return cell.frame.size.height + cell.textView.frame.origin.y + cell.textView.frame.size.height + cell.showMoreButton.frame.size.height + 20;
}
}
Теперь происходит то, что я вижу, как пользовательская ячейка меняет размер своего текстового представления, однако таблица не обновляет высоту строки для этой конкретной ячейки.Если проверить оператор If-else, то выясняется, что hasBeenResized всегда имеет значение false, хотя я установил его на YES в IBACtion CustomCell.
Я рассмотрел другие решения здесь, но все они, похоже,задействовать didSelectRowAtIndexPath, который я не могу использовать в этом случае (у меня есть другое поведение для ячейки, когда она выделена).
Я делаю это совершенно неправильно?В идеале, мне бы хотелось, чтобы кнопка «Показать больше» анимировалась вниз при развертывании текстового представления и наоборот при его свертывании.
Спасибо!