iOS uitableview在重新加载数据时冻结
我使用nsarray存储所有tableview单元格,更新单元格后,我调用[tableview reloadData];然后应用程序将被冻结。我怎样才能防止呢 =========================================================================iOS uitableview在重新加载数据时冻结,ios,iphone,uitableview,reloaddata,Ios,Iphone,Uitableview,Reloaddata,我使用nsarray存储所有tableview单元格,更新单元格后,我调用[tableview reloadData];然后应用程序将被冻结。我怎样才能防止呢 ========================================================================= - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)ind
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
NSUInteger nodeCount = _newsFeeds.count;
if (cell == nil) {
CustomCell *customCell = [customCells objectAtIndex:indexPath.row - 1];
[customCell setDelegate:self];
[customCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[customCell displayImages];
cell = customCell;
}
return cell;
}
- (void)getData {
[[BackendUtil shareInstance] getData];
}
- (void)updateData:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
NSMutableArray<Data *> *listOfData = [userInfo objectForKey:@"data"];
_listOfData = listOfData;
[self createCustomCell:listOfData completion:^(NSArray *arr) {
customCells = [arr mutableCopy];
[_tableView reloadData];
}];
}
- (void)createCustomCell:(NSMutableArray<Data *> *)listOfData completion:(void(^)(NSArray *arr))completion {
NSMutableArray *dummyArr = [[NSMutableArray alloc] init];
for (int i = 0 ; i < [data count] ; i++) {
Data *data = [listOfData objectAtIndex:i];
CustomCell *customCell = [[CustomCell alloc] initWithFrame:CGRectMake(posX, posY, width, height) data:data];
[dummyArr addObject:customCell];
}
completion(dummyArr);
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:identifier];
NSUTEGER nodeCount=\u newsFeeds.count;
如果(单元格==nil){
CustomCell*CustomCell=[customCells objectAtIndex:indexath.row-1];
[customCell setDelegate:self];
[自定义单元格设置选择样式:UITableViewCellSelectionStyleNone];
[自定义单元格显示图像];
单元格=自定义单元格;
}
返回单元;
}
-(void)获取数据{
[[BackendUtil shareInstance]getData];
}
-(无效)更新数据:(NSNotification*)通知{
NSDictionary*userInfo=notification.userInfo;
NSMutableArray*listOfData=[userInfo objectForKey:@“数据”];
_listOfData=listOfData;
[self-createCustomCell:listOfData completion:^(NSArray*arr){
customCells=[arr mutableCopy];
[_tableView重载数据];
}];
}
-(void)createCustomCell:(NSMutableArray*)listOfData完成:(void(^)(NSArray*arr))完成{
NSMutableArray*dummyArr=[[NSMutableArray alloc]init];
对于(int i=0;i<[数据计数];i++){
数据*数据=[listOfData对象索引:i];
CustomCell*CustomCell=[[CustomCell alloc]initWithFrame:CGRectMake(posX,posY,width,height)数据:data];
[dummyArr addObject:customCell];
}
完成(dummyArr);
}
为什么要在这里递减索引行?索引路径已经是零基的。当调用此行获取tableview的第一行时,它将向您的NSArray发送一个负索引,这反过来会使您的应用程序崩溃。您在
tableview中加载的是哪种类型的数据?类似于facebook的新闻提要、许多文本和一些图像。CustomCell类是否有一个reuseIdentifier?你的代码不能正常工作,因为你不想重复使用。(出于某种原因,)冰点看起来怎么样。。。它是卡在某个地方还是留下一个空白视图?卡在某个地方。重新加载表格大约需要10秒钟,我希望用户在重新加载表格时可以继续滚动表格。这是因为在表格中的这些单元格之前还有其他自定义单元格array@FaiFabio在这种情况下,除了执行将导致崩溃的非法操作外,您的代码在逻辑上是不正确的。在您发布的代码中,我看不到对其他自定义单元格的任何引用。我只是跳过了那些不能公开的代码,现在,我的应用程序没有崩溃。当它重新加载tableview时,它将被冻结。重新加载过程完成后,它将继续。然后您必须处理隐藏代码中的第0行大小写,因为您发布的代码肯定会崩溃。当您随机省略代码时,很难猜到任何东西。如果有大量单元格,则updateData方法可能会花费太长时间,从而导致冻结。我可以给你的一点建议是,在显示之前通过将单元格粘贴到数组来“预加载”单元格几乎从来都不是填充TableView的方法。必须在CellForRowatineXpath方法中需要单元格时创建单元格,并且必须使用内置的重用机制进行缓存。如果在UITableView中使用多个单元格类型,则应该有案例来处理特定的单元格类型。此链接可能有助于在UITableView中找出两种不同类型的自定义UITableViewCell:
CustomCell *customCell = [customCells objectAtIndex:indexPath.row - 1];