gpt4 book ai didi

iOS:如何在自定义单元格上正确使用 prepareForReuse?将编辑切换到非编辑模式时,我的单元格被错误地重复使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:09 26 4
gpt4 key购买 nike

好吧,这是我关于这个主题的最后一篇文章。我已经尽可能地缩小了它的范围,但我肯定需要帮助来解决这个问题。由于我使用的是自定义单元格和嵌入式表格 View ,因此当键盘出现并隐藏一些文本字段(每个单元格都有一个文本字段)时,我必须自己进行滚动。效果很好。但是,如果我的表格 View 向上移动了那么远,一些单元格隐藏在我的标题后面或导航 Controller 栏后面,一旦我结束编辑模式,一切都会变得一团糟。然后之前的隐藏单元格立即处于正常状态并且不会缩进。这看起来很讨厌,我不知道如何解决。如果没有细胞被隐藏,当然一切看起来都很好。

所以我查看了文档并找到了 prepareForReuse,我想这会有所帮助。知道如何解决这个问题吗?如果有人能给我必要的提示,我将非常感激......

我有一个自定义的 UITableViewCell,只有这个方法:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
if (editing){
self.title.hidden = NO;
self.titleLabel.hidden = YES;

self.iconButton.hidden = NO;
self.icon.hidden = YES;
self.costs.hidden = YES;
self.disclosureIndicator.hidden = YES;
self.subcategories.hidden = YES;
} else if (!editing){
self.title.hidden = YES;
self.titleLabel.text = self.title.text;
self.titleLabel.hidden = NO;

self.iconButton.hidden = YES;
self.icon.hidden = NO;
self.subcategories.hidden = NO;
self.costs.hidden = NO;
self.disclosureIndicator.hidden = NO;
}
}

在这里初始化并重用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Data model and cell setup
static NSString *CellIdentifier = @"MainCategoryCell";
MainCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];

...

return cell;
}

由于我使用的是嵌入式表格 View ,因此我必须自己滚动隐藏的文本字段:

- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

//Has to be unregistered always, otherwise nav controllers down the line will call this method
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
//All table views are embedded in the parent view
//The parent view y is defined by the status and navigation bar, the height by the tab bar
CGRect viewRect = self.view.frame;
CGRect tableRect = self.tableView.frame;

//The keyboard size will be adjusted that the height is really only the height overlapping the table
NSDictionary* info = [aNotification userInfo];
CGSize kbOriginalSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat effectiveKeyBoardHeight = kbOriginalSize.height - TABBARHEIGHT - (viewRect.size.height - tableRect.size.height - tableRect.origin.y); //the last origin property is important to find out if there is a header
CGSize kbSize = CGSizeMake(kbOriginalSize.width, effectiveKeyBoardHeight);

//Now the content insets will be adjusted for the calculated part of the keyboard overlapping the table
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// I've changed the apple code here! They use viewRect..in my app this doesn't make any sense, tableRect is key
tableRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(tableRect, self.activeField.frame.origin) ) {
[self.tableView scrollRectToVisible:self.activeField.frame animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}

最佳答案

我认为问题可能是 tableview contentInset 的设置不会调用 tableView 的 prepareForReuse 方法或任何其他方法来调用 cellForRow,这会导致隐藏的单元格出现奇怪的状态。所以在键盘隐藏方法中,配置所有可能显示单元格状态,然后重新加载数据将有助于正确显示单元格。

关于iOS:如何在自定义单元格上正确使用 prepareForReuse?将编辑切换到非编辑模式时,我的单元格被错误地重复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357027/

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