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

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];

[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;


}

这就是一个左划删除的一个学习

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