编辑模式下的 UITableViewCell 选择。
Posted
技术标签:
【中文标题】编辑模式下的 UITableViewCell 选择。【英文标题】:UITableViewCell selection in editing mode. 【发布时间】:2015-03-17 01:18:28 【问题描述】:我正在制作一个自定义 UIViewController,我想在点击 UITableViewCell 时显示一条消息。我创建一个 UITableView 并设置属性
tableView.allowsSelectionDuringEditing = true
即使该属性已设置为 true,也不会调用 tableView(:didSelectRowAtIndexPath) 来显示消息。为什么会这样,我该如何解决?
这是我的 UITableViewController:
class GameListViewController: UIViewController, UITableViewDataSource, GameListViewDelegate, UITableViewDelegate
private var _games: [GameObject] = []
var tableView: UITableView return (view as GameListView).tableView
var gameListView: GameListView return (view as GameListView)
override func loadView()
view = GameListView(frame: UIScreen.mainScreen().bounds)
gameListView.delegate = self
override func viewDidLoad()
super.viewDidLoad()
tableView.dataSource = self
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return _games.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
var index: Int = indexPath.row
let currentGame = _games[index]
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByCharWrapping
cell.textLabel?.numberOfLines = 2
if currentGame.isFinished == true
cell.imageView?.image = UIImage(named: "Finished.png")
cell.textLabel?.text = "Winner: Player\(currentGame.playerMakingMove)\nMissiles Launched: \(currentGame.missileCount)"
else
cell.imageView?.image = UIImage(named: "Resume.png")
cell.textLabel?.text = "Turn: Player\(currentGame.playerMakingMove)\nMissiles Launched: \(currentGame.missileCount)"
return cell
/*Handles deleting the cells*/
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
if (editingStyle == UITableViewCellEditingStyle.Delete)
var index: Int = indexPath.row
_games.removeAtIndex(index)
tableView.deleteRowsAtIndexPaths(NSArray(array: [indexPath]), withRowAnimation: UITableViewRowAnimation.Left)
tableView.reloadData()
/* Performs when a cell is touched */
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
var index: Int = indexPath.row
tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
println("inside tableView: didSelectRowAtIndexPath")
var message = UIAlertView(title: "Row selected", message: "You have selected a row", delegate: nil, cancelButtonTitle: "Click ME!", otherButtonTitles: "no other buttons")
message.show()
func makeNewGame()
addGames(1)
tableView.reloadData()
func addGames(gameNumber: Int)
var p1 = Player()
var p2 = Player()
for var i = 0; i < gameNumber; i++
var randBool = Bool( round(drand48())) // True/False
var randPlayer:Int = Int( round( (drand48() + 1)) ) // 1 or 2
var randMissleCount:Int = Int( arc4random_uniform(5000) + 1 ) //1 - 5001
_games.append(GameObject(isFinished: randBool, playerMakingMove: randPlayer, missileCount: randMissleCount, player1: p1, player2: p2))
这是包含 UITableView 的 UIView:
protocol GameListViewDelegate: class
func makeNewGame()
class GameListView: UIView, PictureButtonDelegate
var newGameButton: PictureButton!
var tableView: UITableView!
weak var delegate: GameListViewDelegate? = nil
override init(frame: CGRect)
super.init(frame: frame)
newGameButton = PictureButton(frame: CGRect(), fileName: "NewGame.png")
newGameButton.backgroundColor = UIColor.grayColor()
newGameButton.delegate = self
newGameButton.setTranslatesAutoresizingMaskIntoConstraints(false)
tableView = UITableView()
tableView.allowsSelectionDuringEditing = true
tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(newGameButton)
self.addSubview(tableView)
override func layoutSubviews()
let views: [String : UIView] = ["button": newGameButton, "tableView": tableView]
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[button]-|", options: .allZeros, metrics: nil, views: views))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[tableView]-|", options: .allZeros, metrics: nil, views: views))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(>=15,<=25)-[button]-[tableView]-|", options: .allZeros, metrics: nil, views: views))
func buttonPushed()
delegate?.makeNewGame()
required init(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
【问题讨论】:
只显示必要的代码。 【参考方案1】:您必须为UITableView
设置一个委托并实现func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
。
您只设置数据源,但从不设置委托(GameListView
仅设置一个 GameListViewDelegate
)。
将GameListViewController
中的viewDidLoad
函数更改为:
override func viewDidLoad()
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
【讨论】:
以上是关于编辑模式下的 UITableViewCell 选择。的主要内容,如果未能解决你的问题,请参考以下文章
UITableviewCell - 在编辑模式下单击空复选标记圆圈不会选择单元格
如何在 UITableView 的编辑模式下只显示复选标记选择而不选择整个 UITableViewCell?
UITableViewCell 在编辑模式下移动时显示两个不同的项目
iPhone UITableViewCell selectionStyle 编辑时?