gpt4 book ai didi

iphone - 触摸时更新 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:52 25 4
gpt4 key购买 nike

我有一个应用程序,我可以在 UITableView 中加载图像。每行都有一个 ImageView 。

  1. 我从 URL 下载图像并将其分配给 UIImageView。这里的问题是,如果我拖动表格 View , ImageView 不会更新。它仅在我松开手指时更新。

  2. 另一个问题是我在所有单元格中都有一个标签显示一个使用计时器递增的计数器。当我的手指放在表格 View 上时,也不会调用计时器操作。

在这两种情况下我该如何进行更新?

可能这是一个非常基本的问题。但我不知道。

谢谢大家!

编辑: 我注意到 -connection:didReceiveResponse: 方法在 View 时没有被调用正在被触摸。

编辑 2: 我尝试使用 NSURLConnection 添加运行循环。我的方法如下所示。

- (void)start {

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:self.imageURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:self.timeoutInterval];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

NSPort* port = [NSPort port];
NSRunLoop* rl = [NSRunLoop currentRunLoop]; // Get the runloop
[rl addPort:port forMode:NSRunLoopCommonModes];
[_connection scheduleInRunLoop:rl forMode:NSRunLoopCommonModes];
[_connection start];
[rl run];
}

即使我正在触摸 View ,这也会加载图像。但是表格 View 滚动得非常慢。它甚至没有滚动。它只会在我拖动时移动。松开手指后没有任何 Action 。

最佳答案

问题一

解决方案是使用在您拖动表格 View 时不会被阻止的下载机制。 <强> SDWebImage 是一个异步图像下载器,具有 UIImageView 类别的缓存支持。即使您正在拖动表格,这也会让您的 ImageView 得到更新。我在使用 SDWebImage 的项目中遇到了同样的问题。只需像这样调用即可。

 [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

问题2

如果在主线程跟踪触摸时它的 scheduledTimer 不会被调用。做这样的事情。只需创建一个计时器并添加到循环中

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这不会打断你的计时器。

关于iphone - 触摸时更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16662423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com