UITableViewCell按钮标签在UISearchController中返回所需的IndexPath,而不是FetchedResultsController [英] UITableViewCell button tags return desired IndexPath in UISearchController, not FetchedResultsController
问题描述
我在Swift 2.0的Core Data项目中的 UITableView
上实现了 NSFetchedResultsController
。另外,我实现了 UISearchController
。除了我在自定义 UITableViewCell
按钮上遇到的行为之外,一切都正常运行。
I implemented an NSFetchedResultsController
on a UITableView
in a Core Data project in Swift 2.0. Additionally, I have a UISearchController
implemented. Everything works perfectly with the exception of the behavior I'm encountering on my custom UITableViewCell
buttons.
当 UISearchController
是活动的, customTableViewCell
的按钮可以正常工作。如果在 fetchedResultsController
显示结果时单击同一按钮,则无论我单击哪个按钮,该方法都认为索引0是发送方。
When UISearchController
is active, the customTableViewCell
's buttons work as they should. If I click the same button when the fetchedResultsController
is displaying its results, the method thinks Index 0 is the sender, regardless of which button I click.
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
的缩写版本,以显示我正在设置单元格的按钮:
Here's an abbreviated version of my cellForRowAtIndexPath
to show I'm setting up the cells' buttons:
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
等,没有太多运气。当我单击模拟器中的按钮时,所有在编译器中看起来很有希望的东西都崩溃了。
I've tried a few other approaches I've found here, such as translating CGPoints
to IndexPaths
, etc. without much luck. Everything that looked promising in the compiler crashed when I clicked the button in the simulator.
感谢您阅读。
更新
安装Xcode 7.1,重新启动,清理缓存,删除衍生数据,进行冷启动。
Update Installed Xcode 7.1, rebooted, cleaned caches, nuked derived data, did a cold boot.
解决方案
在很多情况下,标记都能完成工作(例如在 Array
),并在这里获得很多选票,但据我了解,它们并非一直有效。谢谢蒙迪(Mundi)为我提供了一个更强大的解决方案。
Tags will get the job done in many cases (such as getting the location in an Array
) and get lots of votes here, but as I've learned, they don't work all the time. Thank you to Mundi for pointing me towards a more robust solution.
// 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
推荐答案
我已经尝试了一些我在这里发现的其他方法,例如将CGPoints转换为IndexPaths等,但是运气不佳。
I've tried a few other approaches I've found here, such as translating CGPoints to IndexPaths, etc. without much luck.
确实是最可靠的解决方案。 此答案包含正确的代码。
Translating points is indeed the most robust solution. This answer contains the correct code.
这篇关于UITableViewCell按钮标签在UISearchController中返回所需的IndexPath,而不是FetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!