Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UITableViewCell选择处于编辑模式。_Uitableview_Swift_Event Handling_Uikit - Fatal编程技术网

UITableViewCell选择处于编辑模式。

UITableViewCell选择处于编辑模式。,uitableview,swift,event-handling,uikit,Uitableview,Swift,Event Handling,Uikit,我正在制作一个自定义UIViewController,我想在点击UITableViewCell时显示一条消息。我创建UITableView并设置属性 tableView.allowselectionduringediting=true 即使该属性已设置为true,也不会调用tableView(:didSelectRowatineXpath)来显示消息。这是为什么?我该如何修复它 这是我的UITableViewController: class GameListViewController: UI

我正在制作一个自定义UIViewController,我想在点击UITableViewCell时显示一条消息。我创建UITableView并设置属性

tableView.allowselectionduringediting=true

即使该属性已设置为true,也不会调用tableView(:didSelectRowatineXpath)来显示消息。这是为什么?我该如何修复它

这是我的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))
    }

}
类GameListViewController:UIViewController、UITableViewDataSource、GameListViewDelegate、UITableViewDelegate
{
私有var_游戏:[游戏对象]=[]
var tableView:UITableView{return(作为GameListView.tableView查看)}
var gameListView:gameListView{return(作为gameListView查看)}
重写func loadView()
{
视图=游戏列表视图(框架:UIScreen.mainScreen().bounds)
gameListView.delegate=self
}
重写func viewDidLoad()
{
super.viewDidLoad()
tableView.dataSource=self
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
{
return\u games.count
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格
{
变量索引:Int=indexath.row
让currentGame=_games[索引]
变量单元格:UITableViewCell=UITableViewCell(样式:UITableViewCellStyle.Default,reuseIdentifier:nil)
cell.textlab?.lineBreakMode=NSLineBreakMode.ByCharWrapping
cell.textLabel?.numberOfLines=2
如果currentGame.isFinished==真
{
cell.imageView?.image=UIImage(名为:“Finished.png”)
cell.textlab?.text=“获胜者:玩家\(currentGame.PlayerMachingMove)\n发射的导弹:\(currentGame.MisselCount)”
}
其他的
{
cell.imageView?.image=UIImage(名为:“Resume.png”)
cell.textlab?.text=“回合:玩家\(currentGame.PlayerMachingMove)\n发射的导弹:\(currentGame.MisselCount)”
}
返回单元
}
/*处理删除单元格的操作*/
func tableView(tableView:UITableView,CommittedItingStyle编辑样式:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath)
{
if(editingStyle==UITableViewCellEditingStyle.Delete)
{
变量索引:Int=indexath.row
_games.removeAtIndex(索引)
tableView.deleteRowsAtIndexPaths(NSArray(数组:[indexPath]),带RowAnimation:UITableViewRowAnimation.Left)
}
tableView.reloadData()
}
/*触摸单元格时执行*/
func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath)
{
变量索引:Int=indexath.row
tableView.SelectRowatingIndexPath(indexPath,动画:true,scrollPosition:UITableViewScrollPosition.None)
println(“内部表视图:didSelectRowAtIndexPath”)
var message=UIAlertView(标题:“选定的行”,消息:“您已选定一行”,委托:nil,取消按钮提示:“单击我!”,其他按钮提示:“无其他按钮”)
message.show()
}
func makeNewGame()
{
游戏介绍(1)
tableView.reloadData()
}
func addGames(游戏编号:Int)
{
var p1=Player()
var p2=Player()
对于变量i=0;i
}

以下是我的UIView,其中包含UITableView:

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")
}
}
协议GameListViewDelegate:类
{
func makeNewGame()
}
类GameListView:UIView,PictureButtoneLegate
{
新游戏按钮:图片按钮!
var tableView:UITableView!
弱变量委托:GameListViewDelegate?=nil
重写初始化(帧:CGRect)
{
super.init(frame:frame)
newGameButton=PictureButton(框架:CGRect(),文件名:“NewGame.png”)
newGameButton.backgroundColor=UIColor.grayColor()
newGameButton.delegate=self
newGameButton.SetTranslatesAutoResizengMaskintoConstraints(false)
tableView=UITableView()
tableView.AllowSelectionDuringEdit=true
tableView.SetTranslatesAutoResizengMaskintoConstraints(false)
self.addSubview(新建游戏按钮)
self.addSubview(tableView)
}
覆盖func布局子视图()
{
让视图:[字符串:UIView]=[“按钮”:新游戏按钮,“tableView”:tableView]
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“H:|-[button]-|”,选项:。全零,度量:零,视图:视图))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“H:|-[tableView]-|”,选项:。全零,度量:零,视图:视图))

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“V:|-(>=15,您必须为
UITableView
设置一个委托,并实现
func tableView(tableView:UITableView,didSelectRowAtIndexPath:NSIndexPath)

您仅设置数据源,而不设置委托(对于
GameListView
,仅设置
GameListView的
GameListView委托)

GameListViewController
中的
viewDidLoad
功能更改为:

override func viewDidLoad()
{
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
}

只显示必要的代码。