gpt4 book ai didi

iOS:何时调用 cellForRowAtIndexPath?还是标签会更新?

转载 作者:行者123 更新时间:2023-11-29 10:51:27 24 4
gpt4 key购买 nike

我开始迷糊了。我正在为我的 TableView 数据使用 FetchedResultsController。在每个单元格中,我都有一个按钮和一个在 cellForRowAtIndexPath 方法中用 indexPath.Row 标记的文本字段:

- (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];

/* ... */

cell.title.tag = indexPath.row;
cell.iconButton.tag = indexPath.row;

return cell;
}

现在,对于 Fetched Results Controller ,我的行移动方法有点复杂。但是我很确定标签在移动后不会更新。这是正常的吗? cellForRow 方法是否仅在创建新单元格后调用?我必须在移动方法中自己更新标签吗?我怎样才能在那里访问单元格中对象的标签属性?

- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath;
{
// Process the row move. This means updating the data model to correct the item indices.

//reordering has been defined in the CoreDataViewController so the
//FetchedResultsController doesn't mess up the reordering since he would update
//the fetched results permanently while reordering
self.reordering = YES;

//Makes only a mutable copy of the array, but NOT the objects (references) within
NSMutableArray *fetchedResults = [[self.fetchedResultsController fetchedObjects] mutableCopy];

// Grab the item we're moving
NSManagedObject *resultToMove = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];

// Remove the object we're moving from the array.
[fetchedResults removeObject:resultToMove];
// Now re-insert it at the destination.
[fetchedResults insertObject:resultToMove atIndex:[destinationIndexPath row]];

// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.
int i = 1;
for (MainCategory *fetchedResult in fetchedResults)
{
fetchedResult.position = [NSNumber numberWithInt:i++];
}

// Save
NSError *error = nil;
[self.budgetDatabase.managedObjectContext save:&error];

// re-do the fetch so that the underlying cache of objects will be sorted
// correctly
[self.fetchedResultsController performFetch:&error];

self.reordering = NO;
}

最佳答案

是的,移动单元格时标签不会更新是正常的。由于所有单元格都有可能被移动,只需重新加载表格 View 即可让它为您的按钮和文本框重新生成标签。

[tableView reloadData];

关于iOS:何时调用 cellForRowAtIndexPath?还是标签会更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274382/

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