objective c - iPhone :UITableView CellAccessory Checkmark -
in iphone app on click of table view cell want display table view cell accessory type check mark on didselectrowatindexpath writing code
if(indexpath.row ==0) { [tableview cellforrowatindexpath:indexpath].accessorytype=uitableviewcellaccessorycheckmark; }
and displaying check marks.
but in case wnt allow user check on cell @ time means if user select other row row should checked , checked should unchecked
how can achieve that?
keep track, in instance variable, of row checked. when user selects new row, first uncheck checked row, check new row , update instance variable.
here more detail. first add property keep track of checked row. it's easiest if nsindexpath
.
@interface rootviewcontroller : uitableviewcontroller { ... nsindexpath* checkedindexpath; ... } ... @property (nonatomic, retain) nsindexpath* checkedindexpath; ... @end
in cellforrowatindexpath
add following:
if([self.checkedindexpath isequal:indexpath]) { cell.accessorytype = uitableviewcellaccessorycheckmark; } else { cell.accessorytype = uitableviewcellaccessorynone; }
how code tableview:didselectrowatindexpath:
depend on behavior want. if there must row checked, is, if user clicks on checked row use following:
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // uncheck previous checked row if(self.checkedindexpath) { uitableviewcell* uncheckcell = [tableview cellforrowatindexpath:self.checkedindexpath]; uncheckcell.accessorytype = uitableviewcellaccessorynone; } uitableviewcell* cell = [tableview cellforrowatindexpath:indexpath]; cell.accessorytype = uitableviewcellaccessorycheckmark; self.checkedindexpath = indexpath; }
if want allow user able uncheck row clicking on again use code:
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // uncheck previous checked row if(self.checkedindexpath) { uitableviewcell* uncheckcell = [tableview cellforrowatindexpath:self.checkedindexpath]; uncheckcell.accessorytype = uitableviewcellaccessorynone; } if([self.checkedindexpath isequal:indexpath]) { self.checkedindexpath = nil; } else { uitableviewcell* cell = [tableview cellforrowatindexpath:indexpath]; cell.accessorytype = uitableviewcellaccessorycheckmark; self.checkedindexpath = indexpath; } }
Comments
Post a Comment