问题
I am working on a project where I have to implement same as news application. I mean initially all cells will contain News Title and when user clicks on any title then the content will display by expanding the cell. Please suggest some tutorials for this. I am using following code for expansion of cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
if (isChild) {
NSLog(@"A child was tapped, do what you will with it");
return;
}
[self.tableView beginUpdates];
if (currentExpandedIndex == indexPath.row) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
else {
BOOL shouldCollapse = currentExpandedIndex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
[self expandItemAtIndex:currentExpandedIndex];
}
[self.tableView endUpdates];
}
回答1:
Here are few which is very good to implement expendable cell :
https://github.com/OliverLetterer/SLExpandableTableView
https://github.com/singhson/Expandable-Collapsable-TableView
回答2:
Try this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Set expanded cell then tell tableView to redraw with animation
self.expandedRow = indexPath;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:self.expandedRow]) {
return EXPANDED_HEIGHT;
}
return NORMAL_HEIGHT;
}
来源:https://stackoverflow.com/questions/28740263/uitableview-expandable-dynamic-cell-height-according-to-the-uitextview-inside