【发布时间】:2023-03-22 01:47:01
【问题描述】:
我见过一个叫 ZuluTrade 的应用,它的 tableView 单元格中有一个图表。
基本上好像是tableViewCell的imageView中的图片。
当页面加载时,tableView 首先显示文本,然后在我们滚动 tableView 时将图像加载到 tableViewCell 中。
这有助于提高性能,因为用户不必等到所有内容都加载到单元格中,并且它使 tableView 滚动平滑。
如何实现该功能?
以下是应用截图:
我的tableView中的代码cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
//if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
NSString *testVal = [[NSUserDefaults alloc] valueForKey:@"idValue"];
if (testVal == (NSString *)[NSNull null]) {
testVal = @"NULL";
}
//NSLog(@"testVal : %@",testVal);
NSLog(@"Fund Name : %@",[[array1 objectAtIndex:indexPath.row] valueForKey:@"FundName"]);
NSDate *date = [NSDate date];
NSDateFormatter *dtFormat = [[NSDateFormatter alloc] init];
[dtFormat setDateFormat:@"yyyy-MM-dd"];
NSString *dtString = [dtFormat stringFromDate:date];
NSString *fundname = [NSString stringWithFormat:@"%@",[[array1 objectAtIndex:indexPath.row] valueForKey:@"FundName"]];
NSString *imageName =[NSString stringWithFormat:@"%@_%@.png",fundname,dtString];
//NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [self getImagePath];
NSLog(@"Image Path : %@",documentsDirectory);
NSError *error1;
NSString *filepath1;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error1];
if (files == nil) {
NSLog(@"Error reading contents of documents directory: %@", [error1 localizedDescription]);
}
NSLog(@"FileName: %@",imageName);
BOOL success = NO;
for (NSString *file in files)
{
NSLog(@"file in Files is %@",file);
if([file isEqualToString:[NSString stringWithFormat:@"%@",imageName]])
{
filepath1 = [documentsDirectory stringByAppendingPathComponent:file];
NSLog(@"Full Path :%@",filepath1);
success = YES;
}
}
if(success == YES)
{
cell.imageView.image = [UIImage imageWithContentsOfFile:filepath1];
}
else if(success != YES)
{
cell.imageView.image = [UIImage imageNamed:@"newfund.png"];
}
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 120.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.textColor = [UIColor blackColor];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
//NSLog(@"%@",[[array1 objectAtIndex:indexPath.row] valueForKey:@"FundName"]);
label.text = [NSString stringWithFormat:@"%@",[[array1 objectAtIndex:indexPath.row] valueForKey:@"FundName"]];
//NSLog(@"FundName: %@",label.text);
label.textAlignment = UITextAlignmentCenter;
//label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
return cell;
}
【问题讨论】:
-
你应该为 -tablView:cellForRowAtIndexPath 方法编写你的代码,这样我们就可以提供如何改进它的提示。
-
@Parth,做一个快速回顾,我会在你找到你要找的文件后添加一个 break 语句。另外,您为什么要创建一个全新的字符串进行比较?你应该做
if([file isEqualToString:imageName]) -
@PARTH,这些 png 文件有多大?
-
@Black Frog:png 文件大小为 4 KB。
-
@Black Frog:我试着按照你说的改变一切,但仍然没有提高性能。现在我想将图像加载为 Lazy Table Images,但图像位于我的应用程序的 Document 目录中。那么有没有一种方法可以使用 Lazy table 图像呢?
标签: iphone objective-c cocoa-touch uitableview ios4