如何加载图像在tableView后一点延迟? [英] How to load images in tableView after a bit of delay?

查看:101
本文介绍了如何加载图像在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] contentOfDirectoryAtPath:documentsDirectory error:& error1];
if(files == nil){
NSLog(@读取文档目录的内容:%@,[error1 localizedDescription]);
}
NSLog(@FileName:%@,imageName);
BOOL success = NO;
for(NSString * file in files)
{
NSLog(@文件中的文件是%@,file);
if([file isEqualToString:[NSString stringWithFormat:@%@,imageName]])
{
filepath1 = [documentsDirectory stringByAppendingPathComponent:file];
NSLog(@完整路径:%@,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;
}


解决方案



您可以使用以下



看看下面的SDWebImage存储库链接。



https://github.com/rs/ SDWebImage#readme



这将帮助你。



现在,您需要进行一个小的更改:

  cell.imageView setImageWithURL:[NSURL fileURLWithPath:test1.png] 
placeholderImage:[UIImage imageNamed:@placeholder.png]];

代替

  [cell.imageView setImageWithURL:[NSURL URLWithString:test1.png] 
placeholderImage:[UIImage imageNamed:@placeholder.png]];

快乐帮助: - )


I have seen an app called ZuluTrade, which has a charts in there tableView cells.

Basically it seems to be images in tableViewCell's imageView.

When the page loads the tableView displays the text first and then it loads the images into the tableViewCell as we scroll through the tableView.

This helps to improve the performance because the user does not have to wait till all things are loaded into the cell and also it makes tableView scrolling smooth.

How can that feature be achieved?

Here are the screenshots of the app:

Code in my 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;
}

解决方案

You can add images from documents directory and add them late into the table view as Lazy table images.

For that you can use the following

Take a look at the SDWebImage repository link below.

https://github.com/rs/SDWebImage#readme

This would help you out.

Now there you need to make one small change:

[cell.imageView setImageWithURL:[NSURL fileURLWithPath:test1.png]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

in place of

[cell.imageView setImageWithURL:[NSURL URLWithString:test1.png]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Happy to Help :-)

这篇关于如何加载图像在tableView后一点延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆