我在swift
中使用自定义单元格和VLCPlayer
创建了一个iOS应用。在我的项目中,我将点击特定的单元格,VLCPlayer
将根据该单元格中的fileID
下载视频。
当VLCPlayer
未显示任何流式传输时,UITableView
工作正常,并且didSelectRowAtIndexPath
会立即调用。
我的问题是:
在我的VLCPlayer
成为播放流之后,我必须在该单元格中按住3秒才能调用didSelectRowAtIndexPath
,但我希望它立即调用。
有谁能告诉我如何处理这个问题?
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TimelineTableViewCell
let motionImage:UIImage!
motionImage = UIImage (named: "motion.png")
if self.items[indexPath.row].image == nil
{
cell.snapshotImage.image = UIImage(named: "loading_page.png")
}
else
{
cell.snapshotImage.image = self.items[indexPath.row].image
}
cell.motionImage.image = motionImage
cell.dayLabel.text = self.items[indexPath.row].text
cell.timeLabel.text = self.items[indexPath.row].date
//update the cell image
if self.items[indexPath.row].image == nil
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)){
//1. trying to get the image from cache
var image: AnyObject? = timelineImageCache.objectForKey(userDefaults.stringForKey("cameraName")! + self.items[indexPath.row].fileID! + fileIdentifer)
//2. trying to get the image from filesystem
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let folderPath = path.stringByAppendingPathComponent("timelineImage/")
let destinationPath = folderPath.stringByAppendingPathComponent(userDefaults.stringForKey("cameraName")! + self.items[indexPath.row].fileID! + fileIdentifer + ".jpg")
var fileManager = NSFileManager.defaultManager()
if image == nil
{
if (fileManager.fileExistsAtPath(destinationPath))
{
image = UIImage(contentsOfFile: destinationPath)
println("File System")
}
}
//3. if there's no cache and not exist in file system, then download it
if image == nil
{
image = WebstorageConnection.getVideoSnapshot(self.items[indexPath.row].fileID!)
//imageCache[timeframe.fileID!] = image
timelineImageCache.setObject(image!, forKey: userDefaults.stringForKey("cameraName")! + self.items[indexPath.row].fileID! + fileIdentifer)
FileSystemManager.instance.saveImageToLoacl(image as! UIImage, fileName: userDefaults.stringForKey("cameraName")! + self.items[indexPath.row].fileID! + fileIdentifer)
println("Done Download")
}
dispatch_async(dispatch_get_main_queue()){
cell.snapshotImage.image = image as? UIImage
//println("UI")
}
}
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
let url = WebstorageConnection.getDirectDownloadURL(fileID: items[indexPath.row].fileID!)
vlcPlayer.stop()
vlcLoadingIndicator.startAnimating()
let media : VLCMedia = VLCMedia(URL: url) as VLCMedia
vlcPlayer.media = media
vlcPlayer.play()
}
谢谢大家!
答案 0 :(得分:0)
您要禁用didSelectRowAtIndexPath
3秒钟。玩完之后。正确的吗?
var isAllowed = Bool() // default is true
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
let url = WebstorageConnection.getDirectDownloadURL(fileID: items[indexPath.row].fileID!)
vlcPlayer.stop()
vlcLoadingIndicator.startAnimating()
let media : VLCMedia = VLCMedia(URL: url) as VLCMedia
vlcPlayer.media = media
vlcPlayer.play()
isAllowed = false
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)){
NSThread.sleepForTimeInterval(3) // sleeps for 3 sec.
dispatch_async(dispatch_get_main_queue()){
isAllowed = true
}
}
}
或使用计时器
var isAllowed = Bool() // default is true
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
let url = WebstorageConnection.getDirectDownloadURL(fileID: items[indexPath.row].fileID!)
vlcPlayer.stop()
vlcLoadingIndicator.startAnimating()
let media : VLCMedia = VLCMedia(URL: url) as VLCMedia
vlcPlayer.media = media
vlcPlayer.play()
isAllowed = false
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("yourSelector"), userInfo: nil, repeats: false)
}
func yourSelector() {
isAllowed = true
}
这将完成魔术并且不允许用户在indexPath ...
下选择单元格func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
{
//return nil if you dont want to allow selection
if (isAllowed == false)
{
return nil
}
return indexPath
}