UITableView可扩展+动态单元格高度(根据UITableViewCell内部的UITextView)
问题描述:
我正在一个项目中执行与新闻应用程序相同的项目.我的意思是说,最初所有单元格都将包含新闻标题",并且当用户单击任何标题时,内容将通过扩展单元格来显示.请为此建议一些教程.我正在使用以下代码扩展单元格.
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];
}
答
尝试此代码
- (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;
}