uitableview - UITableViewCell selection in editing mode. -
i making custom uiviewcontroller want show message when uitableviewcell tapped. create uitableview , set property
tableview.allowsselectionduringediting = true
even though property has been set true, tableview(:didselectrowatindexpath) not being called display message. why , how fix it?
here uitableviewcontroller:
class gamelistviewcontroller: uiviewcontroller, uitableviewdatasource, gamelistviewdelegate, uitableviewdelegate { private var _games: [gameobject] = [] var tableview: uitableview {return (view gamelistview).tableview} var gamelistview: gamelistview {return (view 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 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 cell 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 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() var = 0; < 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)) } }
}
here uiview contains 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") } }
you have set delegate uitableview
, implement func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath)
.
you set datasource, never delegate (only gamelistviewdelegate
gamelistview
).
change viewdidload
function in gamelistviewcontroller
to:
override func viewdidload() { super.viewdidload() tableview.datasource = self tableview.delegate = self }
Comments
Post a Comment