【问题标题】:UITableViewCell button tags return desired IndexPath in UISearchController, not FetchedResultsControllerUITableViewCell 按钮标签在 UISearchController 中返回所需的 IndexPath,而不是 FetchedResultsController
【发布时间】:2016-01-23 15:49:05
【问题描述】:

我在 Swift 2.0 的 Core Data 项目中在 UITableView 上实现了 NSFetchedResultsController。此外,我还实现了UISearchController。除了我在自定义 UITableViewCell 按钮上遇到的行为外,一切都运行良好。

UISearchController 处于活动状态时,customTableViewCell 的按钮会正常工作。如果我在fetchedResultsController 显示其结果时单击相同的按钮,则无论我单击哪个按钮,该方法都会认为索引 0 是发件人。

func playMP3File(sender: AnyObject) {

    if resultsSearchController.active {
        // ** THIS WORKS **
        // get a hold of my song
        // (self.filteredSounds is an Array)
        let soundToPlay = self.filteredSounds[sender.tag]
        // grab an attribute
        let soundFilename = soundToPlay.soundFilename as String
        // feed the attribute to an initializer of another class
        mp3Player = MP3Player(fileName: soundFilename)
        mp3Player.play()
    } else {

        // ** THIS ALWAYS GETS THE OBJECT AT INDEX 0 **
        let soundToPlay = fetchedResultsController.objectAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: (view.superview?.tag)!)) as! Sound
        // OTHER THINGS I'VE TRIED
        // let soundToPlay = fetchedResultsController.objectAtIndexPath(NSIndexPath(forRow: sender.indexPath.row, inSection: (view.superview?.tag)!)) as! Sound
        // let soundToPlay: Sound = fetchedResultsController.objectAtIndexPath(NSIndexPath(index: sender.indexPath.row)) as! Sound
        let soundFilename = soundToPlay.soundFilename as String
        mp3Player = MP3Player(fileName: soundFilename)
        mp3Player.play()
    }
}

这是我的cellForRowAtIndexPath 的缩写版本,显示我正在设置单元格的按钮:

let customCell: SoundTableViewCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! SoundTableViewCell

if resultsSearchController.active {
    let sound = soundArray[indexPath.row]
    customCell.playButton.tag = indexPath.row
} else {
    let sound = fetchedResultsController.objectAtIndexPath(indexPath) as! Sound
    customCell.playButton.tag = indexPath.row
}

    // add target actions for cells
    customCell.playButton.addTarget(self, action: "playMP3file:", forControlEvents: UIControlEvents.TouchUpInside)

我尝试了一些我在这里找到的其他方法,例如将CGPoints 转换为IndexPaths 等,但运气不佳。当我单击模拟器中的按钮时,编译器中看起来很有希望的所有内容都崩溃了。

感谢您的阅读。

更新 安装 Xcode 7.1,重新启动,清理缓存,核对派生数据,进行冷启动。

解决方案

在许多情况下,标签可以完成工作(例如在Array 中获取位置)并在这里获得大量选票,但据我所知,它们并非一直都有效。感谢 Mundi 为我提供更强大的解决方案。

// this gets the correct indexPath when resultsSearchController is not active
let button = sender as! UIButton
let view = button.superview
let cell = view?.superview as! SoundTableViewCell
let indexPath: NSIndexPath = self.tableView.indexPathForCell(cell)!
let soundToPlay = fetchedResultsController.objectAtIndexPath(indexPath) as! Sound

【问题讨论】:

  • 有什么理由让你以与其他方式相同的方式净获取标签? view.superview?.tag 而不是 sender.tag。在我看来,您应该始终使用“sender.tag”
  • let soundToPlay = fetchedResultsController.objectAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: (view.superview?.tag)!)) as! Sound in playMP3File let sound = fetchedResultsController.objectAtIndexPath(indexPath) as! Sound in cellForRowAtIndexPath 显示相同的结果?
  • 表中有多少个部分?
  • 表格中有 1 个部分。我已经尝试将该部分硬编码为 0。真正让我失望的是,除了 ****ing 按钮之外,有关单元的所有内容都可以正常工作。我知道这是标签的问题。我不是第一个这样做的人,所以我一定是最愚蠢的。
  • let sound = fetchedResultsController.objectAtIndexPath(indexPath) as! Sound 崩溃。在lldb,如果我po sender 我得到0po indexPath 返回0po sender.indexPath 返回fatal error: unexpectedly found nil while unwrapping an Optional value fatal error: unexpectedly found nil while unwrapping an Optional value

标签: ios uitableview core-data uibutton nsfetchedresultscontroller


【解决方案1】:

我尝试了一些我在这里找到的其他方法,例如将 CGPoints 转换为 IndexPaths 等,但运气不佳。

翻译点确实是最强大的解决方案。 This answer 包含正确的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多