小米5splus缺陷怎么点上边

> 博客详情
在日常开发中我们可能会遇到需要自定义UITableViewCell左滑删除按钮的样式,网上也有许多自定义的第三方,但是都太重量级了,应为我们可能我们的需求很小,也不想大动干戈的导入一个第三方,然后设置各种一大堆属性,太麻烦了,那么怎么来修改系统自带的呢?
可能你说不能修改,万是不是绝对的,我们有神器reveal,作为一名iOS程序猿,如果你连reveal都不知道或不会用的话你就太low了,OK,我们开始我们的黑暗料理.
首先我们来看reveal下UITableViewCell左滑按钮的层级关系,
我们来看下这是cell左滑状态下的层级关系
1,cell有两个subView,一个是UITableViewCellDeleteConfirmationView,这个类我们在xcode中是打不出来也找不出来的,所以大家就别找了
另一个是cell的contentView,这个就是我们常用的cell.contentView,这个就不用说了大家都知道,
我们来分析UITableViewCellDeleteConfirmationView,他是一个view,他的子空间就是我们要找的删除按钮,
大家看到_UITableViewCellActionButton这个类别担心,我们不用管它,我们把他看做UIButton就行了,当做button那就好办了,想怎么改就怎么改,那么我们怎么拿到这个按钮呢,且看下面黑暗料理
-(void)modifiDeleteBtn{
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
subView.backgroundColor=kColorB
for (UIButton *btn in subView.subviews) {
if ([btn isKindOfClass:[UIButton class]]) {
btn.backgroundColor=kColorB
遍历子控件就找到了
然后想怎么改就怎么改,
这段代码我们要在
layoutSubviews里调用,才能遍历出来
UITableViewCellDeleteConfirmationView的宽度我们怎么控制呢,
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除哈哈哈哈哈";
在tableView的这个代理里面控制,然后在cell内部控制我们按钮要显示的title,也可以设置image,也可以把它给remove调,添加我们自己定义的view啊什么的,
删除按钮是添加在
UITableViewCellDeleteConfirmationView 这个类对应的view上面的,所以大家添加的时候也添加到他上面,布局大小就按他的frame来设置,也可以添加约束.有兴趣的同学试试
人打赏支持
码字总数 647
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥【IOS开发】IOS开发,tableview中得cell滑块删除
(原文地址:.cn/s/blog_rpsz.html)
在IOS开发中,tableview的滑动删除还是很有用的,上午搜索了很久,发现都写的不是很清楚,我下面把自己的实现方法写下来,大家一起交流。
由于之前一直没做过tableview的删除和添加,如果有写错的地方请指出来。
如果想实现滑动(轻扫)cell右边就能显示一个删除按钮,则要实现tableview
的datasource的方法:
(1)- (void)tableView:(UITableView
*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath
*)indexPath&
此方法就是给一个空的实现滑动也会出现删除按钮!!
可能我们会对按钮的出现和消失的时刻感兴趣,那么此刻要实现代理的方法:
(2)- (void)tableView:(UITableView
*)tableView
willBeginEditingRowAtIndexPath:(NSIndexPath
*)indexPath {
(3)- (void)tableView:(UITableView
*)tableView
didEndEditingRowAtIndexPath:(NSIndexPath
*)indexPath {
在删除按钮显示出来之前会调用(2)方法,给我们处理问题的时间。
其实上述方法的调用顺序就是 (2)---&(1)-----&(3)
在1方法中,我们就是用来删除cell,一般调用方法
[tableView
deleteRowsAtIndexPaths:[NSArray
arrayWithObjects:indexPath,
withRowAnimation:UITableViewRowAnimationTop];
后面动画随意,如果想让自己的数据源一起改变,则在调用删除cell之前,记住一定是之前,在源的数组或者字典中删除对应的记录。
下面是我的实现可以参考:
- (void)tableView:(UITableView
*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle ==
UITableViewCellEditingStyleDelete) {
NSLog(@"%d",
indexPath.row);
[self.myArray&removeObjectAtIndex:[indexPath
&[tableView
deleteRowsAtIndexPaths:[NSArray
arrayWithObjects:indexPath,
withRowAnimation:UITableViewRowAnimationTop];
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。5146人阅读
tableviewcell删除按钮实现
转载请注明本文出自surfaceeee的博客(http://blog.csdn.net/u/article/details/),请尊重他人的辛勤劳动成果,谢谢!
本人一枚新入行的iOS程序猿,苦于tableviewcell左划删除按钮自定义(需要自定义图片)的方法,网上寻找多日未果,遂闭关两日,琢磨得此方法,第一次写博客,各位看官轻拍!废话少说,直入正题。
实现效果如图: & &
注意:此种方法适用于单个按钮功能自定义,但不适用于左划出现多个按钮的自定义(其实是我在赶项目,也没想如何多个按钮= =),而且可以点击任意地方取消删除操作,cell返回原来的位置
本方法实现的原理是将自定义按钮加在tableViewCell.contentView的屏幕外的frame上,打个比方,如果是5系的话,那么你自定义按钮的frame的起点就在(320+,0)(320+表示大于等于320...),当你滑动,整个cell往左偏移的时候,这时候本应该右边显示为这样:
但是由于我们把自定义的按钮add到屏幕外,此时contentView上的自定义按钮就将原删除按钮给遮住了。然后接下来就是实现删除了,重写-(void)tableView:(UITableView
*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath方法,有了indexPath就可以将cell删除了,然后刷新tableView即可
贴上代码:
覆写的tableViewCell的初始化方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_deleteView=[[UIView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, 100)];
_deleteView.backgroundColor=BaseViewBackgroundC
[self.contentView addSubview:_deleteView];
ButtonItem *deleteBtn=[[ButtonItem alloc]initWithFrame:CGRectMake(0, 0, 80, 100) WithImageName:@&icon_delete& WithImageWidth:48 WithImageHeightPercentInItem:.7 WithTitle:NSLocalizedString(@&DeleteOrder&, nil) WithFontSize:14 WithFontColor:[UIColor blackColor] WithGap:-5];
&span style=&white-space:pre&& &/span&//ButtonItem是我自己定义的一个控件,下面有介绍,&span style=&font-family: Arial, Helvetica, sans-&&@&icon_delete&是删除按钮的图片&/span&
[_deleteView addSubview:deleteBtn];
覆写tableView的代理方法:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[mutableArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
当然如果只是这样,你会发现你左划完成之后,你的按钮只会有原delete按钮的宽度,此时就可以再通过如下代理方法进行宽度的调节...
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @&通过改变title的长度,你想多宽就多宽&;
ButtonItem是我自己写的一个继承于UIControl的控件,上面有UIImageView和UILable,便于我实现那种带图片和文字的按钮
- (id)initWithFrame:(CGRect)frame WithImageName:(NSString *)imageName WithImageWidth:(CGFloat)imgWidth WithImageHeightPercentInItem:(CGFloat)imgPercent WithTitle:(NSString *)title WithFontSize:(CGFloat)fontSize WithFontColor:(UIColor *)color WithGap:(CGFloat)gap{
self.backgroundColor=[UIColor clearColor];
self=[super initWithFrame:frame];
if (self) {
_imageView=[[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-imgWidth)/2, 5, imgWidth, imgPercent*frame.size.height)];
if (imageName) {
_imageView.image=[UIImage imageNamed:imageName];
_imageView.contentMode=UIViewContentModeScaleAspectF
[self addSubview:_imageView];
_label=[[UILabel alloc]initWithFrame:CGRectMake(0, gap+_imageView.frame.size.height, frame.size.width, (1-imgPercent)*frame.size.height)];
_label.text=
_label.textColor=
_label.textAlignment=NSTextAlignmentC
_label.font=[UIFont systemFontOfSize:fontSize];
[self addSubview:_label];
至此,自定义tableView删除按钮就全部完成了,其实也就是就原delete按钮进行覆盖,然后根据你要的宽度设置title的长度;点击任意地方取消删除也是直接用的系统的方法。
第一次写博客,语言比较混乱,希望大家谅解。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:5186次
排名:千里之外快速实现可自定义的tableView滑动删除功能 - 简书
快速实现可自定义的tableView滑动删除功能
TableView 是自带滑动删除这个功能,但是有时候并不能满足我们的需求。例如,需求要求删除的那个按钮可自定义,这时候系统的功能就嗝屁了...
当然,现在好用的轮子那么多,相信肯定有能满足你要求的。今天,我来教大家怎么保持系统的滑动删除功能不变,又可以随意的自定义删除按钮,快捷方便!
废话不多说,先上图:
delete.gif
开启TableView
的滑动删除功能-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"BtnClick_%zd",indexPath.row);
自定义一个 UITableViewCell实现如下方法:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView *deleteBgView = [UIView new];
deleteBgView.backgroundColor = [UIColor brownColor];
[self.contentView addSubview:deleteBgView];
UIButton *deleteBtn = [UIButton new];
//deleteBtn.backgroundColor = [UIColor yellowColor];
[deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
[deleteBgView addSubview:deleteBtn];
[deleteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset([UIScreen mainScreen].bounds.size.width);
make.top.equalTo(self.contentView);
make.bottom.equalTo(self.contentView).offset(1);
make.width.equalTo(self.contentView);
[deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.offset(80);
make.top.equalTo(deleteBgView);
make.bottom.equalTo(deleteBgView);
make.left.equalTo(deleteBgView);
这里我用了 Masonry 来做布局约束,相信大家一定不陌生,到这里基本完成了,iOS7,8,9 亲测无误,轻松愉快!
注: 这种做法只适用于一个按钮的情况,多个按钮的情况因为无法触发按钮的点击事件,所以无从下手,研究出来的小伙伴,望告知!
随手记下身边精彩的瞬间~

我要回帖

更多关于 小米5splus 刷机 的文章

 

随机推荐