iphone - Custom UITableViewCell redraw issues -
i have custom uitableview cell i've added textbox editing, shows , hides based on edit mode. i've tried adding vertical line shows when editing, , that, i'm running drawing issues. added green checkmark rightview start working on input validation feedback, , i'm seeing similar issues.
here code cell, , part of cellforrowatindexpath.
#import <uikit/uikit.h> @interface editablecellstyle2 : uitableviewcell { cgrect editrect; uitextfield *editfield; uiview *lineview; } @property (nonatomic, readonly, retain) uitextfield *editfield; @property (nonatomic, readonly, retain) uiview *lineview; @end
#import "editablecellstyle2.h" @implementation editablecellstyle2 @synthesize editfield; @synthesize lineview; - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code. editrect = cgrectmake(83, 12, self.contentview.bounds.size.width-83, 19); editfield = [[uitextfield alloc] initwithframe:editrect]; editfield.font = [uifont boldsystemfontofsize:15]; editfield.textalignment = uitextalignmentleft; editfield.textcolor = [uicolor blackcolor]; editfield.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexibleheight; [self.contentview addsubview:editfield]; self.editfield.enabled = no; self.editfield.hidden = yes; lineview = [[uiview alloc] initwithframe:cgrectmake(80, 0, 1, self.contentview.bounds.size.height)]; self.lineview.backgroundcolor = [uicolor lightgraycolor]; [self.contentview addsubview:lineview]; self.lineview.hidden = yes; } return self; } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state. } -(void)layoutsubviews { [super layoutsubviews]; // layouts cell uitableviewcellstylevalue2 editrect = cgrectmake(83, 12, self.contentview.frame.size.width-self.detailtextlabel.frame.origin.x-10, 19); editfield.frame = editrect; } - (void)willtransitiontostate:(uitableviewcellstatemask)state { [super willtransitiontostate:state]; if (state & uitableviewcellstateeditingmask) { self.detailtextlabel.hidden = yes; self.editfield.enabled = yes; self.lineview.hidden = no; self.editfield.hidden = no; } } - (void)didtransitiontostate:(uitableviewcellstatemask)state { [super didtransitiontostate:state]; if (!(state & uitableviewcellstateeditingmask)) { self.editfield.enabled = no; self.editfield.hidden = yes; self.lineview.hidden = yes; self.detailtextlabel.hidden = no; self.editfield.text = self.detailtextlabel.text; } } - (void)dealloc { [editfield release]; [lineview release]; [super dealloc]; } @end
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // handling every section hand since view static. sections 0, 1, 2, , 4 use generic editable cell. // section 3 uses multiline address cell. static nsstring *cellidentifier = @"cell"; editablecellstyle2 *cell = (editablecellstyle2 *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (indexpath.section == 0 || indexpath.section == 1 || indexpath.section == 2 || indexpath.section == 4) { if (cell == nil) { cell = [[[editablecellstyle2 alloc] initwithstyle:uitableviewcellstylevalue2 reuseidentifier:cellidentifier] autorelease]; } } // configure odometer if (indexpath.section == 0) { nsarray *array = [sectionsarray objectatindex:indexpath.section]; nsdictionary *dictionary = [array objectatindex:indexpath.row]; cell.textlabel.text = @"odometer"; cell.detailtextlabel.text = [nsstring stringwithformat:@"%@", [dictionary objectforkey:@"odometer"]]; cell.tag = kodometer; cell.editfield.text = cell.detailtextlabel.text; cell.editfield.placeholder = @"odometer"; cell.editfield.tag = kodometer; cell.editfield.keyboardtype = uikeyboardtypenumberpad; // create view green checkmark odometer input validation , set right view. uiimage *checkimage = [uiimage imagenamed:@"tick.png"]; uiimageview *checkimageview = [[[uiimageview alloc] initwithimage:checkimage] autorelease]; cell.editfield.rightview = checkimageview; cell.editfield.rightviewmode = uitextfieldviewmodealways; } return cell; }
there more cells built same way.
the problems that, when in edit mode, vertical lines display properly. when leave edit mode, cells off screen when go normal mode still have vertical line (it doesn't hidden). also, i've added imageview checkmark indicator, cells off screen when switching modes gain checkmark. (only section 0 sets up).
i've noticed if cell.setneedsdisplay, text label , detail text label won't update if data source has been updated. have [self.tableview reloaddata] skips active animations.
i'm sure these issues related me using custom cell + dequeuereusablecellwithidentifier, can't find what.
any feedback or push in right direction appreciated.
edit: not using reusable cells seems have resolved above issues. i'm still open feedback on cell code. forgot 1 other issue may or may not related. 1 of cells has "tap view list" button. if enter data cells while in edit mode, hit button choose info list (it displays modal table view), when dismiss modal view, of cells' edited data has reverted original state. i'm not calling reload data when dismiss modal view controller. thought might fixed not using reusable cells isn't.
you need prepare cell reuse. try adding editablecellstyle2
implementation:
- (void)prepareforreuse { [super prepareforreuse]; [self didtransitiontostate:uitableviewcellstatedefaultmask]; }
Comments
Post a Comment