gpt4 book ai didi

ios - 在显示最后一个单元格时添加表格单元格。在 ios 中

转载 作者:行者123 更新时间:2023-11-28 22:57:00 26 4
gpt4 key购买 nike

我设置了。当最后一个单元格显示时,通过 threadProcess 添加单元格。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell      forRowAtIndexPath:(NSIndexPath *)indexPath
{
int nArrayCount;
nArrayCount=[self.mAppGameList count];
int row= (int)indexPath.row ;

if(row == nArrayCount)
{
if(mSearchGame_Thread != nil)
return;

NextSearchCell *searchCell =(NextSearchCell *)cell;

[searchCell.mActivityView startAnimating];

NSThread *searchThread = [[NSThread alloc] initWithTarget:self
selector:@selector(searchNextThreadProc:) object:tableView];

self.mSearchGame_Thread = searchThread;
[searchThread release];
[self.mSearchGame_Thread start];
// start new search ...

}

//线程方法

  -(void)searchNextThreadProc:(id)param
{

UITableView *tableView=(id)param;

NSMutableArray *newArray;

newArray=[NSMutableArray arrayWithArray:self.mAppGameList];

NSArray *pressedlist;
nArrayCount=[self.mAppGameList count];

.
.
.
[newArray addObject:item];
self.mAppGameList = newArray;

[tableView reloadData];

self.mSearchGame_Thread=nil;
}

这种方式是有问题的。

  1. 如果我在 tableview 重新加载数据时滚动表格,tableview 会消失并出现一段时间。

  2. 如果我在添加下一个单元格时触摸单元格,有时会发生坏的 exc 内存。我认为,它在重新加载新表之前调用 tableView: didSelectRowAtIndexPath: 方法。所以,表的数据不是。

所以,我想换成reload tableview的方式,请问有什么办法吗?请帮助我。

最佳答案

您可以在 UITableView 中使用此方法通过动画添加新行,而不是使用 reloadData:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

这样您的 View 就不会在重新加载时消失并重新出现。

看到这个问题:UITableView add cell Animation

还要确保在主线程中使用以下方法刷新 UI:

[self performSelectorOnMainThread:@selector(refreshMethodName) withObject:nil waitUntilDone:NO];

关于您的代码的一些评论:

  • 确保您的 mAppGameList 是保留属性或复制属性(如果在您的代码中按上述方式使用)。否则可能会导致无法访问。
  • 您应该确保不会同时多次调用 searchNextThreadProc,否则您可能会遇到计时和性能问题。它看起来不是线程安全的。
  • 通常,您应该将内容数据与 UITableView 分开处理。将 TableView 视为一种工具,用于显示已存在的数据列表。它不应该担心搜索数据等。而是使用一个单独的类来保存您在 NSMutableArray 中使用的数据,您可以在需要时不断填充数据。 tableView 可以通过方法调用触发此类开始搜索新数据,但要确保刷新过程是线程安全的,并且可以从 UI 进行多次调用!一次调用 10xrefresh 仍然意味着一次只刷新 1 次! (例如,我们不希望同时调用 10 个服务器)此内容列表应与 UITableView 的列表完全分开。
  • 当有新数据可用时,通过创建一个您可以调用的刷新方法告诉 UITableView 进行刷新。刷新UITableView时,如果mAppGameList属性为retain或copy,则无需重新添加列表中的所有数据。如果您在单独的类中有一个 NSMutableArray,其中包含所建议的所有数据,只需使用 self.mAppGameList = [NSArray arrayWithArray:[yourClass gameList]]; (如果您正在为 mAppGameList 使用保留)
  • 触发 UITableView 的刷新时,使用 performSelectorOnMainThread。要启动一个新的后台线程,您还可以使用 performSelectorInBackground 而不是 NSThread alloc 等。

关于ios - 在显示最后一个单元格时添加表格单元格。在 ios 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10584835/

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