【发布时间】:2016-11-08 07:06:50
【问题描述】:
好的,我在 Swift 中有一个 iOS 应用程序。我有一个自定义表格视图,在我的单元格中有一个按钮(2 个按钮,但在修复一个时,我可以修复另一个)。单击按钮时,单元格应该从一个数组移动到另一个数组,但我的问题是当我单击按钮时应用程序崩溃说无法识别的选择器。有什么帮助吗?
错误:
CustomCellSwift[4834:1676038] -[CustomCellSwift.ViewController followButtonClick:]: unrecognized selector sent to instance 0x100b0b490
关注按钮代码:
// Follow Button
@IBAction func followButtonClick(sender: UIButton!) {
// Adding row to tag
let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
// Showing Status Labels
let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
cell.firstStatusLabel.isHidden = false
cell.secondStatusLabel.isHidden = false
// Change Follow to Following
(sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
cell.followButton.isHidden = true
cell.followedButton.isHidden = false
self.myTableView.beginUpdates()
// ----- Inserting Cell to Section 0 -----
followedArray.insert(testArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from Section 1 -----
testArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
}
}
填充单元格代码
func populateCell(_ testObject: Test, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {
// Loading Background Color
self.backgroundColor = UIColor.white
// Loading Images -SDWebImage- *Crashes*
//let imgURL = (testObject.value(forKey: "testURL") as! String) // as! NSURL
//self.myImageView.contentMode = .scaleAspectFit
//self.myImageView.sd_setImage(with: imgURL as URL, placeholderImage: UIImage(named: "no-image.png")) // Missing RefreshCached
// Loading Status Labels
self.firstStatusLabel.text = testObject.testStatus1
self.secondStatusLabel.text = testObject.testStatus2
self.firstStatusLabel.isHidden = true
self.secondStatusLabel.isHidden = true
if isFollowed {
self.followedButton.tag = indexPath.row
self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick:")), for: .touchUpInside)
self.followedButton.isHidden = false
self.followButton.isHidden = true
// Status Labels
self.firstStatusLabel.isHidden = false
self.secondStatusLabel.isHidden = false
}
else {
self.followButton.tag = indexPath.row
self.followButton.addTarget(parentView, action: Selector(("followButtonClick:")), for: .touchUpInside)
self.followedButton.isHidden = true
self.followButton.isHidden = false
// Status Labels
self.firstStatusLabel.isHidden = false // True when done testing
self.secondStatusLabel.isHidden = false // True when done testing
}
}
【问题讨论】:
-
为什么要在表格视图单元格中放置一个按钮。难道你不能使用 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 对被选中的表格单元格做出反应,然后移动到新数组?
-
检查你是否在 pinEditor 或代码中连接到 action
followButtonClick,如果你添加了目标。 -
@MacUserT 我不能,设计不是这样
标签: ios swift uitableview compiler-errors uibutton