我有一个应用程序,可以在 UITableView
中加载图像。每行都有一个 ImageView 。
我从 URL 下载图像并将其分配给
UIImageView
。这里的问题是,如果我拖动表格 View , ImageView 不会更新。仅当我松开手指时它才会更新。另一个问题是我在所有单元格中都有一个标签,显示一个使用计时器递增的计数器。当我的手指放在桌面 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 ,也会加载图像。但 TableView 滚动速度非常慢。它甚至不滚动。它仅在我拖动时移动。松开手指后没有任何动静。
最佳答案
问题1
解决方案是使用拖动表格 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/