ios - How to select a cell in table view without scrolling it? -


i building settings screen app using table vc. in viewdidload, want show user his/her current settings selecting cells. there checkmark on left of selected cells. didselectrowatindexpath method:

override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {     let cell = tableview.cellforrowatindexpath(indexpath)     row in 0..<tableview.numberofrowsinsection(indexpath.section) {         let celltobedeselected = tableview.cellforrowatindexpath(nsindexpath(forrow: row, insection: indexpath.section))         celltobedeselected?.accessorytype = .none     }     cell!.accessorytype = .checkmark     tableview.deselectrowatindexpath(indexpath, animated: true) } 

this works well. when select row, checkmark appears. when select row, previous checkmark disappears , new 1 appears.

however, want select rows programmatically in viewdidload, in order show current settings.

i found method called selectrowatindexpath. tried use requires uitableviewscrollposition. docs says:

the position in table view (top, middle, bottom) given row scrolled.

it seems whenever want select row, table view must scroll selected row is. want select it, , only select it.

how this?

this purely depends on logic.

your trying deselect rows first, when row selected. checked current code, found not maintaining state of selected row, cause dequeue problem i.e. when selected cell reused other cell, show checkmark if not selected.

instead of this, need maintain index variable store indexpath of selected row. have 3 sections, , 3selections allowed, in case, need maintain 1 array holds values of selectedindexpaths.

declare 1 property in viewcontroller below.

var arrayselectedindexpaths : array<nsindexpath> = array(); 

in viewdidload: add 3 default value per last selected setting.

suppose last time user have selected

row1 in section0

row2 in section1

row0 in section2

then array follow,

        //in viewdidload         arrayselectedindexpaths.append(nsindexpath(forrow: 1, insection: 0));         arrayselectedindexpaths.append(nsindexpath(forrow: 2, insection: 1));         arrayselectedindexpaths.append(nsindexpath(forrow: 0, insection: 2)); 

so in tableview:cellforrowatindexpath: write below logic. logic show checkmark in cell if selected, when comes onto screen.

if indexpath == self.arrayselectedindexpaths[indexpath.section] {    cell!.accessorytype = .checkmark }else{    cell!.accessorytype = .none } 

and in tableview:didselectedrowatindexpath: write logic below. update indexpath in array per selection. deselect previous row , select new row.

override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {     let cell = tableview.cellforrowatindexpath(indexpath)  //deselected selected cell         let celltobedeselected = tableview.cellforrowatindexpath(self.arrayselectedindexpaths[indexpath.section])         celltobedeselected?.accessorytype = .none      cell!.accessorytype = .checkmark     tableview.deselectrowatindexpath(indexpath, animated: true)     self.arrayselectedindexpaths[indexpath.section] = indexpath;//update new selected indexpath in array } 

i have wrote above logic, per our discussion in comments question. below our discussion.

@sweeper trying show check mark in 1 row, based on last setting value ? – hitendrahckr

@hitendrahckr yes am. trying show check mark in 3 different rows, since there 3 settings – sweeper

so, have 3 sections different 3 settings ? – hitendra hckr

and think can selected in 1 section, right ? – hitendra hckr

@hitendrahckr yes you're right – sweeper


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -