您的位置:首页 > 产品设计 > UI/UE

Adding a custom image to a UITableView delete button

2012-04-09 09:19 411 查看
ApplemakesitdifficultifnotimposibletocustomizeelementssuchasaUITableView’sdeletebutton.WelltodayIfoundawaytobypassallthelimitationsandaddacustomimagetoadeletebutton.NowIfirstwanttowarnyou,thisisnotanofficialwayofdoingthis,wewillbeaddingaUIImageViewoverthedeletebutton’ssubview.Sothatbeingsaid,letsgetstarted!

ThefirstthingweneedtodoissetupaUITableView.

Inyourviewcontroller.mviewdidloadmethodaddthefollowingcode:

UITableView*table=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStylePlain];
[tablesetDataSource:self];
[tablesetDelegate:self];
[self.viewaddSubview:table];
[tablesetEditing:TRUEanimated:TRUE];
[tablerelease];

Nowaddthetableviewdelegatemethodstoyourviewcontroller.m:

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return1;
}

-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

return5;
}

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{

UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]];

if(cell==nil){

cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]]autorelease];

}

cell.textLabel.text=@"txt";

returncell;
}

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];
}


[/code]
Andvoila!Youshouldnowhaveatableviewthatautomaticallyenterseditmodeonlaunch.Butwestillhavethoseuglydefaultdeletebuttons.

Sonowletsstartcustomizing!First,wearegoingtoneedaUITableViewCellsubclass,toaddonegotoFile>NewFilethenselect”Objective-CClass”afterthatgodowntothedrop-downmenuandselect“UITableViewCell”nowchooseanamefortheclassandclick“Finish”.

Nowthatwehaveourcustomcellclass,weneedtopointthetableviewtotheclass.Firstyouneedtoimporttheclassintheviewcontroller.h.

SecondweneedtoreplacetheselinesinthecellForRowAtIndexmethod:

UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]];

if(cell==nil){

  cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]]autorelease];

}

with:

className*cell=(className*)[tableViewdequeueReusableCellWithIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]];

if(cell==nil){

cell=[[[classNamealloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:[NSStringstringWithFormat:@"Cell%i",indexPath.section]]autorelease];

}


[/code]
Wearenowpointingthetableviewtothecustomclass.

Nowforthatlastbutnotleaststep:

Inyourcustomcellclassaddthefollowingmethod:

-(void)willTransitionToState:(UITableViewCellStateMask)state{
  [superwillTransitionToState:state];
  if((state&UITableViewCellStateShowingDeleteConfirmationMask)==UITableViewCellStateShowingDeleteConfirmationMask){
for(UIView*subviewinself.subviews){
  if([NSStringFromClass([subviewclass])isEqualToString:@"UITableViewCellDeleteConfirmationControl"]){
UIImageView*deleteBtn=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,64,33)];
[deleteBtnsetImage:[UIImageimageNamed:@"delete.png"]];
[[subview.subviewsobjectAtIndex:0]addSubview:deleteBtn];
[deleteBtnrelease];
  }
}
 }
}


[/code]
“delete.png”isthenameofthecustomimage,makesuretheimageis64x33pixels.Thisistheresult:








                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐