菜鸟教程小白 发表于 2022-12-11 21:47:19

iphone - UITableView cellForRowAtIndexPath 导致滚动缓慢


                                            <p><p>我的表格滚动非常缓慢,我认为这是由我的 cellForRowAtIndexPath 中的核心数据方法引起的。下面是我的 cellForRowAtIndexPath 方法:</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @&#34;Cell&#34;;

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:@&#34;user_device&#34; 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:@&#34;device&#34; inManagedObjectContext:context];
    ;

    NSPredicate *predicate = ];
    ;
    NSArray *fetchedDevices = ;
    for (NSManagedObject *infod in fetchedDevices) {
      // Get all room
      roomid = ;
      fetchRequest = [ init];
      entity = [NSEntityDescription
                  entityForName:@&#34;room&#34; inManagedObjectContext:context];
      ;

      NSPredicate *predicate = , buildingID];
      ;
      NSMutableArray *fetchedRoom = [ mutableCopy];

      // add room to array if room belongs to selected building and room not already added
      if( &gt; 0) { //&amp;&amp; lastRoomID != ) {
            for (NSManagedObject *info in fetchedRoom) {
                NSLog(@&#34;room id: %@&#34;, );
                // add room ids to array if not already there
                if (!] &amp;&amp; intValue]])
                  ];
            }
            //lastRoomID = ;
      }
    }
}

fetchRequest = [ init];
entity = [NSEntityDescription
          entityForName:@&#34;room&#34; inManagedObjectContext:context];
;

NSPredicate *predicate = ;
;
NSArray *fetchedRoomTotal = ;
//NSLog(@&#34;Total Rooms for Building: %d&#34;, );
//NSLog(@&#34;Scanned Rooms for Building: %d&#34;, );
//NSLog(@&#34;Scanned rooms: %@&#34;, scannedRoomArray);
if( == &amp;&amp; &gt; 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:@&#34;user_device&#34; inManagedObjectContext:context];
;
NSArray *fetchedObjects = ;
NSNumber *deviceid = ;
//NSNumber *roomid = ;

for (NSManagedObject *info in fetchedObjects) {
    // Get all device
    deviceid = ;
    fetchRequest = [ init];
    entity = [NSEntityDescription
            entityForName:@&#34;device&#34; inManagedObjectContext:context];
    ;

    NSPredicate *predicate = , roomID];
    ;
    NSArray *fetchedDevices = ;
    for (NSManagedObject *infod in fetchedDevices) {

      // add device to array
      if( &gt; 0) {
            NSLog(@&#34;room id: %d&#34;, roomID);
            // add device ids to array if not already there
            if (!)
                ;
      }
    }
}

fetchRequest = [ init];
entity = [NSEntityDescription
          entityForName:@&#34;device&#34; inManagedObjectContext:context];
;

NSPredicate *predicate = ;
;
NSArray *fetchedDeviceTotal = ;
//NSLog(@&#34;Total Devices for Room: %d&#34;, );
//NSLog(@&#34;Scanned Devices for Room: %d&#34;, );
//NSLog(@&#34;Scanned Devices: %@&#34;, scannedDeviceArray);
if( == &amp;&amp; &gt; 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]
查看完整版本: iphone - UITableView cellForRowAtIndexPath 导致滚动缓慢