objective c - how to create different settings for each UITableViewCell -
i have tableview multiple sections.
i want make of them selectable, , others not. also, cells selectable should link different viewcontrollers.
does have idea how this?
the best way found manage per-cell information create cell-info class, , have represent data. on class, can flag whether cell selectable, type, handler, etc.
then, after dequeueing cell, prepare display according cell-info backing object.
example:
@interface mycellinfo @property (nonatomic, copy) nsstring* title; @property (nonatomic, copy) nsstring* detail; @property (nonatomic, strong) uiimage* image; @property (nonatomic, getter=isselectable) bool selectable; @property (nonatomic) sel handlingselector; ... @end @implementation mycellinfo @end
this defines example cell-info class holds properties on each cell. ease of use, register cell class in table example identifier in viewdidload
:
[self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier: @"exampleidentifier"];
your table view data source:
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return datasource.count; } - (nsinteger)tableview:(uitableview*)tableview numberofrowsinsection:(nsinteger)section { return [datasource[section] count]; } - (uitableviewcell*)tableview:(uitableview*)tableview cellforrowatindexpath:(nsindexpath*)indexpath { uitableviewcell* cell = [tableview dequeuereusablecellwithidentifier:@"exampleidentifier"]; mycellinfo* cellinfo = datasource[indexpath.section]; if(cellinfo.image == nil) { //show image } else { [cell.textlabel settext:cellinfo.title]; [cell.detailtextlabel settext:cellinfo.detail]; } return cell; }
your table view delegate:
- (cgfloat)tableview:(uitableview*)tableview heightforrowatindexpath:(nsindexpath*)indexpath { mycellinfo* cellinfo = datasource[indexpath.section]; return cellinfo.height; } - (bool)tableview:(uitableview*)tableview shouldhighlightrowatindexpath:(nsindexpath*)indexpath { mycellinfo* cellinfo = datasource[indexpath.section]; return cellinfo.selectable; } - (nsindexpath*)tableview:(uitableview*)tableview willselectrowatindexpath:(nsindexpath*)indexpath { mycellinfo* cellinfo = datasource[indexpath.section]; return cellinfo.selectable ? indexpath : nil; } - (void)tableview:(uitableview*)tableview didselectrowatindexpath:(nsindexpath*)indexpath { mycellinfo* cellinfo = datasource[indexpath.section]; [self performselector:cellinfo.handlingselector withobject:cellinfo]; }
all remains create data , represent each entry mycellinfo
object. used array of arrays type of db, top level array represents sections, while each inner array section's cells, each mycellinfo
object.
Comments
Post a Comment