我设置。显示最后一个单元格时,按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;
}
这种方式是有问题的。
如果在滚动查看tableview的数据时滚动表,一会儿tableview消失并出现。
如果我在添加下一个单元格时触摸单元格,有时会出现不好的ex记忆。
我认为,它会在重新加载新表之前调用tableView:didSelectRowAtIndexPath:方法。因此,表的数据不是。
所以,我想替换重载tableview的方式。有什么办法吗?请帮我。
最佳答案
您可以使用UITableView中的以下方法通过动画添加新行,而不必使用reloadData:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
这样,您的视图就不会在重新加载时消失或重新出现。
看到以下问题:UITableView add cell Animation
还请确保使用以下命令对主线程中的UI进行任何刷新:
[self performSelectorOnMainThread:@selector(refreshMethodName) withObject:nil waitUntilDone:NO];
关于您的代码的一些注释:
如果在代码中如上使用,请确保您的mAppGameList是保留属性或复制属性。否则可能导致访问错误。
您应确保不会一次多次调用searchNextThreadProc,否则可能会出现计时和性能问题。它看起来不是线程安全的。
通常,您应该处理与UITableView分开的内容数据。将表视图作为一种工具来显示已存在的数据列表。它不必担心搜索数据等问题。而是使用一个单独的类将您正在使用的数据保存在NSMutableArray中,并在需要时不断填充数据。可以触发该类以通过tableView通过方法调用开始搜索新数据,但请确保刷新过程是线程安全的,并且对于从UI进行的多次调用而言是可持续的!一次10次刷新呼叫仍然意味着一次只能刷新1次! (例如,我们不希望同时调用10个服务器)此内容列表应与UITableView的列表完全分开。
当有新数据可用时,通过创建可以调用的刷新方法来告诉UITableView刷新。刷新UITableView时,如果保留或复制了mAppGameList属性,则无需重新添加列表中的所有数据。如果您在包含建议的所有数据的单独类中具有NSMutableArray,则只需使用诸如self.mAppGameList = [NSArray arrayWithArray:[yourClass gameList]]之类的东西即可; (如果您对mAppGameList使用retain)
触发UITableView的刷新时,请使用performSelectorOnMainThread。要启动新的后台线程,您还可以使用performSelectorInBackground代替NSThread alloc等。