gpt4 book ai didi

ios - 如何从 View 外获取 UITableViewCell 并将其滚动到 View 中

转载 作者:行者123 更新时间:2023-11-29 03:00:46 25 4
gpt4 key购买 nike

在我的代码中,当用户向列表中添加新项目时,应用程序将其添加到项目数组中,对它们进行排序,然后调用 [tableView reloadData]。由于对列表进行排序,有时新添加的项目会在 View 之外。当新添加的项目不在 View 中时,我将获取它的 UITableViewCell 并将其滚动回 View 中。

cellForRowAtIndexPath 中,我将记录添加到每个单元格的标记中:

cell.tag = listItems.recordID;

addNewItem 方法中添加新项目并对项目进行排序后:

for (int i = 0; i<listItems.count; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
int tag = cell.tag;
if (tag == newItemRecordID)
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}

当单元格不在 View 中时,tag 属性返回 0,我无法获取单元格。

怎么可能得到那个细胞?

最佳答案

我想,使用这样的东西会更好:

for (int i = 0; i < listItems.count; i++)
{
// Replace 'ListItem' with the name of your class.
ListItem *listItem = [listItems objectAtIndex:i];
if (listItem.recordID == newItemRecordID)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
break;
}
}

关于ios - 如何从 View 外获取 UITableViewCell 并将其滚动到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324224/

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