gpt4 book ai didi

ios - UITableviewCell 高度未在滚动时重置

转载 作者:行者123 更新时间:2023-11-29 12:07:33 25 4
gpt4 key购买 nike

我有一个表格 View ,它可以在选择单元格时展开并在再次选择时折叠。当您选择时,单元格应展开并显示标签,当您再次选择时,它会折叠并隐藏标签。展开和折叠工作正常,但如果我在展开单元格后滚动 tableview,它会表现得很奇怪。一旦它离开 View 并返回,该单元格将具有展开单元格的高度,但应该在展开单元格中显示的标签被隐藏。如果我再次选择该单元格,它会折叠并显示标签。我用 ,

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:t cellForRowAtIndexPath:indexPath];
if([self cellIsSelected:indexPath])
return cell.frame.size.height+35;
return cell.frame.size.height;
}

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [self.selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell
NSLog(@"Select cell:%@",indexPath);
[self.tableView deselectRowAtIndexPath:indexPath animated:TRUE];

if([self pickTaskForIndexPath:indexPath].productSpecialMessage){
BOOL isSelected = ![self cellIsSelected:indexPath];
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[self.selectedIndexes setObject:selectedIndex forKey:indexPath];
PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath];
cell.message.hidden=false;
cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage;
cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail;
cell.messageLabel.numberOfLines=3;
if(cell.messageLabel.hidden==true){

cell.messageLabel.hidden = false;
} else {
cell.messageLabel.hidden = true;
}
NSLog(@"message:%@",cell.messageLabel.text);
[cell layoutIfNeeded];
}

self.tableView.rowHeight=UITableViewAutomaticDimension;
[self.tableView beginUpdates];
[self.tableView endUpdates];

}

indexPath 添加到 didSelectRowAtIndexPath 上的 selectedIndexes请帮助我

最佳答案

单元格只能在 cellForRowAtIndexPath 中配置。当发生状态变化使单元格需要看起来不同时,只需重新加载该单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PickTaskTableviewCell *cell = (PickTaskTableviewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];

// everything else you do to configure the cell goes here, then ...

// check the logic here, we want one condition that tells us whether to show the labels
if([[self cellIsSelected:indexPath] && self pickTaskForIndexPath:indexPath].productSpecialMessage){
// don't need these here
//NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
// [self.selectedIndexes setObject:selectedIndex forKey:indexPath];
// PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath];

cell.message.hidden=false;
cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage;
cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail;
cell.messageLabel.numberOfLines=3;
cell.messageLabel.hidden=NO;
} else {
cell.message.hidden=YES;
cell.messageLabel.hidden=YES;
}
NSLog(@"message:%@",cell.messageLabel.text);
// don't need this here
// [cell layoutIfNeeded];
return cell;
}

选择(可能还有取消选择)导致需要更新单元格,所以...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// don't deselect it here, just reload it

// more on this later...
[self.selectedIndexes setObject:selectedIndex forKey:indexPath];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

// probably do the same in didDeselectRowAtIndexPath:

最后一点(可选)。无需维护您自己的选定索引路径列表,UITableView 会为您完成,因此您可以删除 selectedIndexes 属性并仅使用 TableView 方法,例如。 ..

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
return [[self.tableView indexPathsForSelectedRows] containsObject:indexPath];
}

关于ios - UITableviewCell 高度未在滚动时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519162/

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