ios - How to create a dynamic size array -
what trying achieve:
i have uitableview , want check whether table selected or not , keep in array easy access yes or no values corresponds row afterwards can manipulate data.
my code follows
- (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsuinteger row = [indexpath row]; uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; nsstring *celllabeltext = cell.textlabel.text; if (cell.accessorytype == uitableviewcellaccessorycheckmark) { cell.accessorytype = uitableviewcellaccessorynone; selected[row] = no; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; selected[row] = yes; } }
as stands out can create bool selected[some value]
problem max index needed me unknown table size changes constantly. setting max index limits me.
i new objective c , come php background dont know whether possible create array want in objective-c.
otherwise options within objective-c have easy way easy write/read selected[row] = yes/no
.
i need way write yes/no , link indexpath.row
use nsmutableset
, store nsindexpath
of selected rows. if select row add path set. if unselect row, remove path set.
to see if row selected, see if indexpath in set or not.
btw - works if rows fixed. if user can add, remove, or reorder rows approach not work. in such case need store data keys, not index paths.
create ivar of type nsmutableset
. let's call selectedrows
:
selectedrows = [[nsmutableset alloc] init];
then in didselectrow
do:
- (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { bool selected = [selectedrows containsobject:indexpath]; uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; nsstring *celllabeltext = cell.textlabel.text; if (selected) { cell.accessorytype = uitableviewcellaccessorynone; [selectedrows removeobject:indexpath]; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; [selectedrows addobject:indexpath]; } }
in cellforrow...
method similar:
bool selected = [selectedrows containsobject:indexpath]; cell.accessorytype = selected ? uitableviewcellaccessorycheckmark : uitableviewcellaccessorynone;
Comments
Post a Comment