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:
Nowaddthetableviewdelegatemethodstoyourviewcontroller.m:
[/code]
Andvoila!Youshouldnowhaveatableviewthatautomaticallyenterseditmodeonlaunch.Butwestillhavethoseuglydefaultdeletebuttons.
Sonowletsstartcustomizing!First,wearegoingtoneedaUITableViewCellsubclass,toaddonegotoFile>NewFilethenselect”Objective-CClass”afterthatgodowntothedrop-downmenuandselect“UITableViewCell”nowchooseanamefortheclassandclick“Finish”.
Nowthatwehaveourcustomcellclass,weneedtopointthetableviewtotheclass.Firstyouneedtoimporttheclassintheviewcontroller.h.
SecondweneedtoreplacetheselinesinthecellForRowAtIndexmethod:
with:
[/code]
Wearenowpointingthetableviewtothecustomclass.
Nowforthatlastbutnotleaststep:
Inyourcustomcellclassaddthefollowingmethod:
[/code]
“delete.png”isthenameofthecustomimage,makesuretheimageis64x33pixels.Thisistheresult:
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:
相关文章推荐
- How to Make Custom Drawn Gradient Backgrounds in a Grouped UITableView with Core Graphics
- How to Make Custom Drawn Gradient Backgrounds in a Grouped UITableView with Core Graphics
- DataGrid: Adding a Delete Button to Each Row
- DrawerLayout+TabLayout+ViewPager+RadioButton+Fragment+pullToRefresh+ImageLoader
- android 图像淡入淡出资源问题(Button can't be cast to ImageView)
- java.lang.ClassCastException~android.widget.ImageView cannot be cast to android.widget.Button~
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- 开源框架02(PullToRefresh;PinnedHeaderListView;PhotoView;CustomShapeImageView;ormlite)
- 系出名门Android(5) – 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- android中ListView点击和ListView的item里面的button或ImageView不能同时生效的解决
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- iOS8 自定义UITabBar (使用popToViewController导致的UITabBarButton重叠的问题)
- PHP Tutorials : Add, View, Edit & Delete from to MySQL database, Live Example (News System)
- ios uitableview button 获取cell indexpath.row
- how to scroll a big imageView in ScrollView
- UITableView 的一个常见错误'unable to dequeue a cell with identifier Cell - must register a nib or a class f
- iOS-随心所欲控制button内image view和title view的位置
- 跪求大神指教一个ImageButton与surfaceView问题
- UITableView cell上的button和label问题