gpt4 book ai didi

ios - UITableView reloadRowsAtIndexPaths 图形故障

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:24 25 4
gpt4 key购买 nike

如果我为一个部分的第一个单元格调用 reloadRowsAtIndexPaths,而前一节为空而上面的部分不为空,我会遇到一个奇怪的动画故障(即使我指定了“UITableViewRowAnimationNone”),其中重新加载的单元格从以上部分..

我尽量简化了这个例子:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 1;
else if (section == 1)
return 0;
else if (section == 2)
return 3;
return 0;
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
cell.textLabel.text = @"Text";

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *editedCell = [[NSArray alloc] initWithObjects:indexPath, nil];
//[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:editedCell withRowAnimation:UITableViewRowAnimationNone];
//[self.tableView endUpdates];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Section";
}

其实你可以把最后一个方法注释掉,但这样可以更好地理解问题。

最佳答案

您可以直接为单元格设置您想要的值,而不是让表格自行重新加载(从而避免任何不需要的动画)。此外,为了使代码更清晰并避免代码重复,我们将单元设置移动到一个单独的方法(这样我们就可以从不同的位置调用它):

- (void) setupCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
cell.textLabel.text = @"Text"; // Or any value depending on index path
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self setupCell:cell forIndexPath:indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// create cell

// Configure the cell...
[self setupCell:cell forIndexPath:indexPath];

return cell;
}

关于ios - UITableView reloadRowsAtIndexPaths 图形故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345802/

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