gpt4 book ai didi

ios - UITableView 展开和折叠以显示部分或全部单元格

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:14 25 4
gpt4 key购买 nike

我有一个 UITableView,它使用一个高度约为 200 像素的 commentTableCell,它既有文本又有图像。

我希望当未选择一行时,该行的高度约为 65 像素,这将隐藏所有图像,当该行扩展到 200 像素时,commentTableCell 将完全扩展,因此图像在细胞会暴露。

但我遇到的问题是,当所有行的高度折叠到 65 像素时,commentTabelCell 中的任何图像都不应该显示,但我从表格的某些部分显示的单元格中得到重影或部分图像.为什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";

CommentTableCell *cell = (CommentTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 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 = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
}

//Finally set the selected index to the new selection and reload it to expand
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

最佳答案

我有一个替代解决方案。试试这个:

// ----------------------------//

// 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
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]
// will call heightForRow and thus create a stack overflow
if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 200;

return 65;
}

关于ios - UITableView 展开和折叠以显示部分或全部单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11005106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com