ios - UITableView 展开和折叠以显示部分或全部单元格
<p><p>我有 UITableView,它使用一个高约 200 像素的 commentTableCell,它同时包含文本和图像。 </p>
<p>我希望当未选择一行时,该行的高度约为 65 像素,这将隐藏所有图像,当该行扩展到 200 像素时,commentTableCell 将完全展开,因此内部的图像细胞会暴露。 </p>
<p><strong>但是我遇到的问题是,当所有行都折叠到 65 像素的高度时,commentTabelCell 中的任何图像都不应该显示,但是我从表格的某些部分显示的单元格中得到了重影或部分图像.为什么?</strong></p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
CommentTableCell *cell = (CommentTableCell *);
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex == indexPath.row)
{
return 200;
}
else {
return 65;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//The user is selecting the cell which is currently expanded
//we want to minimize it back
if(selectedIndex == indexPath.row)
{
selectedIndex = -1;
withRowAnimation:UITableViewRowAnimationFade];
return;
}
//First we check if a cell is already expanded.
//If it is we want to minimize make sure it is reloaded to minimize it back
if(selectedIndex >= 0)
{
NSIndexPath *previousPath = ;
selectedIndex = indexPath.row;
withRowAnimation:UITableViewRowAnimationFade];
}
//Finally set the selected index to the new selection and reload it to expand
selectedIndex = indexPath.row;
withRowAnimation:UITableViewRowAnimationFade];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我有一个替代解决方案。试试这个:</p>
<pre><code>// ----------------------------//
// Put this somewhere in your .h file:
NSIndexPath *selectedCellIndexPath;
// ----------------------------//
// Set your default row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 65;
}
// In the implementation file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCellIndexPath = indexPath;
// Forces the table view to call heightForRowAtIndexPath
withRowAnimation:UITableViewRowAnimationNone];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Note: Some operations like calling
// will call heightForRow and thus create a stack overflow
if(selectedCellIndexPath != nil && == NSOrderedSame)
return 200;
return 65;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UITableView 展开和折叠以显示部分或全部单元格,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/11005106/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/11005106/
</a>
</p>
页:
[1]