iOS开发中实现UITableView的Cell左划删除等自定义功能
2016-04-20 17:20
851 查看
在我们的app开发当中,经常会用到UITableView 的左滑删除的功能,通常的话效果如下
下面我们就开始实现,类似的做法
首先是tableView的代理方法,各种数据源方法的实现
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@”identifier:%d”,(int)indexPath.row];
TestTableViewCell cell = (TestTableViewCell )[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[TestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell setBackgroundColor:[UIColor whiteColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
然后进行实现的就是TableView 的cell的的高度问题
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 136;
}
下面的方法,看方法名称,我们也可以得出,他就是在询问是不是可以编辑每一个indexPath,返回YES就是可以编辑
-(BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
return YES;
}
然后就是设置cell的编辑样式
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,//禁止编辑
UITableViewCellEditingStyleDelete,//删除编辑
UITableViewCellEditingStyleInsert //插入编辑
};
上面的cell其实是有三种编辑形式的
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void)delete:(NSIndexPath *)indexPath
{
}
然后就是具体编辑删除的方法
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.dataArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
[self.dataArr addObject:@100];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
当然有时候,我们还是自定义我们的删除的title,有时候可能我们是电商的app,里面会有很多的删除商品,或是退出计划的等等,下面就有一个方法来设置title的方法
-(NSString )tableView:(UITableView )tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @”退出计划”;
}
然后当滚动视图就会发生下面的方法a
//当滚动视图发生位移,就会进入下方代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
UIPageControl pageControl = (UIPageControl )[self.view viewWithTag:1000];
}
这就是一个左划删除的一个学习
下面我们就开始实现,类似的做法
首先是tableView的代理方法,各种数据源方法的实现
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@”identifier:%d”,(int)indexPath.row];
TestTableViewCell cell = (TestTableViewCell )[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[TestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell setBackgroundColor:[UIColor whiteColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.m_back setHidden:NO]; [cell.m_back setFrame:CGRectMake(20, 0, 333, 128)]; [cell.m_back setBackgroundColor:[UIColor lightGrayColor]]; [cell.m_back.layer setCornerRadius:5]; [cell.m_button setHidden:NO]; [cell.m_button setFrame:CGRectMake(30, 0, 40, 50)]; [cell.m_button setBackgroundColor:[UIColor cyanColor]]; [cell.m_button setTitle:[NSString stringWithFormat:@"%ld",indexPath.row+1] forState:UIControlStateNormal]; [cell.m_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [cell.m_button addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside]; [cell.m_label setHidden:NO]; [cell.m_label setFrame:CGRectMake(250, 0, 40, 50)]; NSNumber *num = self.dataArr[indexPath.row]; [cell.m_label setText:[NSString stringWithFormat:@"%c",num.intValue]]; [cell.m_label setTextAlignment:NSTextAlignmentCenter]; return cell;
}
然后进行实现的就是TableView 的cell的的高度问题
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 136;
}
下面的方法,看方法名称,我们也可以得出,他就是在询问是不是可以编辑每一个indexPath,返回YES就是可以编辑
-(BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
return YES;
}
然后就是设置cell的编辑样式
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,//禁止编辑
UITableViewCellEditingStyleDelete,//删除编辑
UITableViewCellEditingStyleInsert //插入编辑
};
上面的cell其实是有三种编辑形式的
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void)delete:(NSIndexPath *)indexPath
{
}
然后就是具体编辑删除的方法
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.dataArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
[self.dataArr addObject:@100];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
当然有时候,我们还是自定义我们的删除的title,有时候可能我们是电商的app,里面会有很多的删除商品,或是退出计划的等等,下面就有一个方法来设置title的方法
-(NSString )tableView:(UITableView )tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @”退出计划”;
}
然后当滚动视图就会发生下面的方法a
//当滚动视图发生位移,就会进入下方代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
UIPageControl pageControl = (UIPageControl )[self.view viewWithTag:1000];
CGPoint point = scrollView.contentOffset; NSInteger index = round(point.x/scrollView.frame.size.width); pageControl.currentPage = index;
}
这就是一个左划删除的一个学习
相关文章推荐
- Android中的Handler, Looper, MessageQueue和Thread
- ProcessBuilder与Runtime
- [ios] Storyboard里面的几种Segue区别及视图的切换
- 使用storyboard设置UIButton的圆角
- 苹果4的手机中UIscrollview无法滑动的解决办法
- 提示:413 Request Entity Too Large 的解决方法
- 使 UITableViewCell 的分割线从头开始显示
- 使用easyUI datagrid的filter时,当value是空时候如何过滤
- Android Studio Error : Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
- requestWindowFeature使用详解
- Raw-OS源代码分析之消息系统-Queue_Size
- beginUpdates和endUpdates 实现UITableView的动画块 4000
- 减少设置UI出错率的方法,设置UI的一些列属性在设置内容之前完成
- Amaze UI的一点总结
- android-Junit测试单个activity二-Ui组件
- 取消延迟执行函数 cancelPreviousPerformRequestsWithTarget
- 用Drawable来实现UI元素的间隔
- DataTables错误: requested unknown parameter
- std::deque 在Debug 与Release 下执行的不同
- JAVA基础(26) java.util.Queue的简单用法