【问题标题】:How to expand and collapse cell correctly like accordion in Swift?如何在Swift中正确扩展和崩溃细胞?
【发布时间】:2015-04-23 07:15:34
【问题描述】:

在我的应用程序中,我试图在UITableView 中播放音频文件。当我点击单元格时,它必须展开并播放音频。然后,如果我点击另一个单元格,它应该像手风琴一样,前一个单元格必须折叠并停止音频。但我的实现没有这样做:

import UIKit
import AVFoundation

class AudioTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
var audioPlayer: AVAudioPlayer?
let mp3sPaths = NSBundle.mainBundle().pathsForResourcesOfType("mp3", inDirectory: nil)
var filteredAudios = [AnyObject]()
var mp3names:NSMutableArray = []
var cellTapped:Bool = true
var currentRow = 0;

override func viewDidLoad() {
    super.viewDidLoad()
    for mp3 in mp3sPaths as Array<String> {
        var mp3name = mp3.lastPathComponent
        mp3names.addObject(mp3name.stringByDeletingPathExtension)
    }
    println(mp3names)
    println(mp3sPaths)
    self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int {
    if tableView == self.searchDisplayController!.searchResultsTableView {
        return self.filteredAudios.count
    } else {
        return self.mp3names.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("AudioCell") as AudioCell

    if tableView == self.searchDisplayController!.searchResultsTableView {
        cell.titleLabel.text = filteredAudios[indexPath.row] as? String

    } else {
        cell.titleLabel.text = mp3names[indexPath.row] as? String
    }

    cell.playIcon.text = "▶️"
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var mp3file = mp3sPaths[indexPath.row] as NSString
    var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
    var error: NSError?
    audioPlayer?.stop()
    audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
    audioPlayer?.play()
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
        cell.playIcon.text = "◾️"
    }

    var selectedRowIndex = indexPath
    currentRow = selectedRowIndex.row
    tableView.beginUpdates()
    tableView.endUpdates()
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AudioCell {
        cell.playIcon.text = "▶️"
    }
    audioPlayer?.stop()

    var selectedRowIndex = indexPath
    currentRow = selectedRowIndex.row
    tableView.beginUpdates()
    tableView.endUpdates()
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == currentRow {
        if cellTapped == false {
            cellTapped = true
            return 70
        } else {
            cellTapped = false
            return 40
        }
    }
    return 40
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
    UIView.animateWithDuration(0.25, animations: {
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
    })
}

func filterContentForSearchText(searchText: String) {
    filteredAudios = mp3names as Array
    filteredAudios.filter{($0 as NSString).localizedCaseInsensitiveContainsString("\(searchText)")}
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText(searchString)
    return true
}

func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]! {
    var shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action, indexPath) -> Void in
        tableView.editing = false
        println("shareAction")
    }
    shareAction.backgroundColor = UIColor.grayColor()

    var doneAction = UITableViewRowAction(style: .Default, title: "Done") { (action, indexPath) -> Void in
        tableView.editing = false
        println("readAction")
    }
    doneAction.backgroundColor = UIColor.greenColor()

    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
        tableView.editing = false
        println("deleteAction")
    }

    return [deleteAction, doneAction, shareAction]
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
}
}

截图如下:

有人可以帮忙吗?

【问题讨论】:

  • 好吧,你有两次相同的委托方法函数签名(didSelectRowAtIndexPath)我怀疑 Swift 编译器会让你这样做,但如果这样做,它调用的那个将是未定义的(随机的),正确?
  • 我没有那个两次,一个是didSelect,另一个是didDESELECT
  • 啊 - 我错过了。我什至不知道委托方法。无论如何,我在下面的回答中为您提供了一些想法

标签: ios uitableview swift


【解决方案1】:

我认为您没有在示例中显示所有代码或当前代码。 我已经在 swift 中实现了同样的东西,而且我知道如果处理得当,表格动画效果会非常好。

如果cell frame本身大于表格显示的高度并且有内容,则需要在通过heightForRowAtIndexPath折叠cell时隐藏cell的contentContview中的悬垂子view,否则会显示可见的子view悬垂cell frame折叠单元格后,通过表格中的其他单元格。这可能是您的情况,但从您的文章中很难确定。

但是一次直接设置 alpha 看起来很俗气。您应该使用 UIView.animateWithDuration() 为 cell.contentView 子视图的 alpha 属性设置动画,这些子视图需要在单元格缩小和单元格展开时隐藏,您希望它们在行收缩时淡出并在行展开时淡入。

【讨论】:

  • 我添加了 viewController 文件的完整代码。拜托,你能阅读它并提出你的解决方案吗?
  • 我对图表以及什么与预期的性能感到困惑,所以我仍然不清楚到底哪里出了问题,或者你想要发生什么。无论如何,我不能真正为你编写或设计你的应用程序,或者花时间研究它来加快你所做的一切。
  • 好吧,你不用写app的代码,当然)我只需要实现这样的手风琴表格视图:https://www.youtube.com/watch?v=u34Fw9biD3k&feature=youtu.be。我认为我写的代码足以理解并建议更正
  • 如果我不需要编写代码或应用程序,那么我发布的内容应该足以让您弄清楚您需要做什么。您可以使用旋转仿射变换将箭头按钮旋转 90º(例如 π/2)并通过动画与单元格内容的超大部分的淡入和淡出平行地旋转回 0º,以实现效果。有用。您必须弄清楚的研究和编码部分。我已经在这个答案上投入了比我能腾出更多的时间,而且你可以看到其他人并没有加入帮助。
猜你喜欢
  • 2017-03-22
  • 2016-05-01
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多