我已经自定义了 TableView 单元格,创建了 nib 文件及其相应的 .h 和 .m 文件。除了滚动时速度缓慢之外,一切正常,可能是什么问题,如何避免卡住?
和
obj = [self.listData objectAtIndex: [indexPath row]];
if (obj != nil) {
static NSString *CellIdentifier;
if (obj->status == status1) {
CellIdentifier = @"Cell_italic";
} else if (obj->status == status2) {
CellIdentifier = @"Cell_cent";
} else {
CellIdentifier = @"Cell";
}
custom_cell *cell = (custom_cell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"custom_cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
char file_path[MAX_PATH]; // Holds file path.
memset(file_path, 0, MAX_PATH);
// Get path
// Set Image
UIImage *cellImage = [UIImage imageWithContentsOfFile:fle_path];
if (cellImage != nil) {
[cell.image_view setImage:cellImage];
}
NSString *combined_name = [NSString stringWithCString:obj->combined_name encoding:NSASCIIStringEncoding];
NSString *email = [NSString stringWithCString:obj->email encoding:NSASCIIStringEncoding];
// Set name
if (obj->status == status1)
{
cell.name_label.text = combined_name;
cell.name_label.font = [UIFont fontWithName:@"Georgia-Italic" size:18];
cell.email_label.text = email;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
. . .
. . .
}
dequeueReusableCellWithIdentifier 总是返回 nil,是吗?如果没有定制,我已经看到它多次不是 nil,现在它总是返回 nil。
最佳答案
每次都会加载图像,这就是为什么 UITableView
中的表格滚动卡住的原因,您可以通过多种方式实现延迟加载,请引用以下链接和演示,这可能会对您有所帮助:-
https://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
https://github.com/rs/SDWebImage //README 部分说明如何在您的应用程序中使用它。
https://github.com/nicklockwood/AsyncImageView/
您还可以使用 UI 线程在后台进程中加载图像
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(backgroundQueue,^{
// background process
image = [NSData dataWithContentsOfFile:imageName];
dispatch_async(mainQueue,^{
// always update GUI from the main thread
// uiimageview.image = image.... etc
});
});
关于iphone - 如何自定义 UITableViewCell 而不会在 UITableView 中出现任何卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766490/