iphone - UITableView cellForRowAtIndexPath 导致滚动缓慢
<p><p>我的表格滚动非常缓慢,我认为这是由我的 cellForRowAtIndexPath 中的核心数据方法引起的。下面是我的 cellForRowAtIndexPath 方法:</p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = ;
if (cell == nil) {
cell = [[ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSManagedObject *info = ];
// all rooms have been scanned for building
if( intValue]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
];
}
else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
];
}
];
];
return cell;
}
</code></pre>
<p>这里是 allRoomsScanned 方法和 allDevicesScanned 方法:</p>
<pre><code>- (BOOL) allRoomsScanned: (int) buildingID {
NSMutableArray *scannedRoomArray = [ init];
// Get all user_device
NSManagedObjectContext *context = ;
NSError *error;
NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"user_device" inManagedObjectContext:context];
;
NSArray *fetchedObjects = ;
NSNumber *deviceid = ;
NSNumber *roomid = ;
//int lastRoomID = 0;
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = ;
fetchRequest = [ init];
entity = [NSEntityDescription
entityForName:@"device" inManagedObjectContext:context];
;
NSPredicate *predicate = ];
;
NSArray *fetchedDevices = ;
for (NSManagedObject *infod in fetchedDevices) {
// Get all room
roomid = ;
fetchRequest = [ init];
entity = [NSEntityDescription
entityForName:@"room" inManagedObjectContext:context];
;
NSPredicate *predicate = , buildingID];
;
NSMutableArray *fetchedRoom = [ mutableCopy];
// add room to array if room belongs to selected building and room not already added
if( > 0) { //&& lastRoomID != ) {
for (NSManagedObject *info in fetchedRoom) {
NSLog(@"room id: %@", );
// add room ids to array if not already there
if (!] && intValue]])
];
}
//lastRoomID = ;
}
}
}
fetchRequest = [ init];
entity = [NSEntityDescription
entityForName:@"room" inManagedObjectContext:context];
;
NSPredicate *predicate = ;
;
NSArray *fetchedRoomTotal = ;
//NSLog(@"Total Rooms for Building: %d", );
//NSLog(@"Scanned Rooms for Building: %d", );
//NSLog(@"Scanned rooms: %@", scannedRoomArray);
if( == && > 0) {
return YES;
}
else {
return NO;
}
}
- (BOOL) allDevicesScanned: (int) roomID {
NSMutableArray *scannedDeviceArray = [ init];
// Get all user_device
NSManagedObjectContext *context = ;
NSError *error;
NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"user_device" inManagedObjectContext:context];
;
NSArray *fetchedObjects = ;
NSNumber *deviceid = ;
//NSNumber *roomid = ;
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = ;
fetchRequest = [ init];
entity = [NSEntityDescription
entityForName:@"device" inManagedObjectContext:context];
;
NSPredicate *predicate = , roomID];
;
NSArray *fetchedDevices = ;
for (NSManagedObject *infod in fetchedDevices) {
// add device to array
if( > 0) {
NSLog(@"room id: %d", roomID);
// add device ids to array if not already there
if (!)
;
}
}
}
fetchRequest = [ init];
entity = [NSEntityDescription
entityForName:@"device" inManagedObjectContext:context];
;
NSPredicate *predicate = ;
;
NSArray *fetchedDeviceTotal = ;
//NSLog(@"Total Devices for Room: %d", );
//NSLog(@"Scanned Devices for Room: %d", );
//NSLog(@"Scanned Devices: %@", scannedDeviceArray);
if( == && > 0) {
return YES;
}
else {
return NO;
}
}
</code></pre>
<p>关于如何消除滚动时的延迟有什么想法吗?我假设我的核心数据调用或在 cellForRowAtIndexPath 中调用方法的方式可能效率低下。</p>
<p>感谢您的帮助。非常感谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你真的不应该在滚动表格 View 时在主线程上进行 Fetch Requests 和处理。
正如 rokjarc 所说,您绝对应该保存(相当繁重的) <code>allRoomsScanned</code> 方法的结果。我建议添加一种新样式,即带有事件指示器,当您还没有该输入的结果时,单元格会获得该样式。加载完成后,您刷新表格 View 单元格。</p>
<p><strong>注意:您不能在 <code>allRoomsScanned</code> 和 <code>allDevicesScanned</code> 中使用默认的 NSManagedObjectContext。</strong>
您需要在后台线程中初始化一个新的上下文。要么在 block 的开头初始化一个新的上下文并将其作为方法参数传递,要么在方法中创建一个新的。</p>
<pre><code>NSManagedObject *info = ];
NSNumber *cachedResult = ;
if (cachedResult == nil) {
// style loading state
int scanInfo = [ intValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL result = ;
forKey:info.objectID];
withRowAnimation:UITableViewRowAnimationAutomatic]
});
} else if (cachedResult.boolValue == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
];
} else if (cachedResult.boolValue == NO) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
];
}
];
];
return cell;
</code></pre></p>
<p style="font-size: 20px;">关于iphone - UITableView cellForRowAtIndexPath 导致滚动缓慢,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9622776/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9622776/
</a>
</p>
页:
[1]