ios - Could not pass parameter to UITableViewCell -
i have different cells , such purpose need pass parameter uiviewcontroller subclass of uitableviewcell. not work. scenario written below:
messagesviewcontroller.m :
#import "messagesviewcontroller.h" #import "messagetableviewcell.h" - (void)viewdidload { [super viewdidload]; [self.tableview registerclass:[messagetableviewcell class] forcellreuseidentifier:messengercellidentifier]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { messagetableviewcell *cell = (messagetableviewcell *)[self.tableview dequeuereusablecellwithidentifier:messengercellidentifier]; if (cell == nil) { cell = [[messagetableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:messengercellidentifier customparam:yes]; } return cell; }
messagetableviewcell.m :
- (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier customparam:(bool)customparam { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // **** custom parameter (customparam) here ??? ****/ } return self; }
at scenario customparam parameter. seems ok, cell not nil , procedure fails.
option 1: remove dequeuereusablecellwithidentifier
line create new custom cell every time. otherwise using pre-existing cell has previous customparameter
set whatever last cell displayed set to.
-note option 1 (added explanation of why very, very, very bad idea (@duncan c). since setting cells reuseidentifier
in creating cells ios hold on them once scroll offscreen can reuse them when code asks it. code never asks reusable cells because makes new 1 each time table asks next cell. causes high load times (to create new cell every time) , high memory use (since os saving cells use later , not deallocating them immediately). reusability built reason, don't use option 1 unless have specific need (and then, wrong, don't it).
option 2: change custom parameter separate method call. instead of in initializer create new method clears cell , rebuilds way new custom parameter requires. can re-use cells , modify looks using new setcustomparameter:
method.
edit: code example of option 2, simple possible:
in table controller
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { messagetableviewcell *cell = (messagetableviewcell *)[self.tableview dequeuereusablecellwithidentifier:messengercellidentifier]; if (cell == nil) { cell = [[messagetableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:messengercellidentifier]; } [cell setcustomparam:customparam]; return cell; }
in cell .m
-(void)setcustomparam:(paramtype)type { //do whatever right here clear previous //cell's custom information , add new custom information //to new cell. }
Comments
Post a Comment