我有一个UIViewController
实现TableViews委托和数据源协议.现在我想向细胞添加"轻扫删除"手势.
我应该怎么做呢.
我给出了commitEditingStyle
方法的空白实现,并将Editing属性设置为YES.
仍然没有滑动功能.
现在我需要单独添加UISwipeGesture
到每个单元格吗?
或者我错过了什么?
我已经和Swift和iOS合作了好几个月了.我熟悉许多方法,但我不够好,我可以不用看就写出来.我很欣赏Stack Overflow过去提供的快速答案,让我回到正轨,我已经生锈了(例如,AsyncTask Android示例).
iOS UITableView
对我来说属于这一类.我已经完成了几次,但我忘记了细节.我在StackOverflow上找不到另一个问题,只是要求一个基本的例子,我正在寻找比许多在线教程更短的东西(虽然这个非常好).
我将在下面提供一个答案供我将来参考和你的.
我想UITableViewCell
在点击编辑按钮或滑动UITableView
行时更改减号按钮的颜色并删除按钮.到目前为止,我已实现此代码:
-(IBAction)doEdit:(id)sender
{
[[self keyWordsTable] setEditing:YES animated:NO];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
Run Code Online (Sandbox Code Playgroud) 我需要更改当我尝试从UITableView
后设置编辑中删除行时出现的默认删除按钮的标题YES
.
我想模仿滑动删除UITableViewCell的功能,就像iOS 8中的邮件应用程序一样.我不是指滑动以显示删除按钮.我指的是当你刷卡时,它会破坏3个动作,但是如果你继续向左滑动,那么电子邮件就会被删除.
在iOS 8中,UITableView有一个新方法,您可以在其中提供数据以显示任意数量的按钮:
#ifdef __IPHONE_8_0
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *viewStackRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Stack" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View Stack Action");
}];
viewStackRowAction.backgroundColor = [UIColor radiusBlueColor];
UITableViewRowAction *viewUserRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"User" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"View User Action");
}];
viewUserRowAction.backgroundColor = [UIColor radiusLightBlueColor];
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
SM_LOG_DEBUG(@"Delete");
}];
deleteRowAction.backgroundColor = [UIColor redColor];
return @[deleteRowAction, viewUserRowAction, viewStackRowAction];
}
#endif
Run Code Online (Sandbox Code Playgroud)
我没有看到任何API来检测你是否继续刷卡.我在UITableView.h中使用了8_0,上面的方法似乎是唯一的新方法.
我想可以监视滚动视图偏移,或添加/劫持UIPanGestureRecognizer.我只是想确保使用默认方式,如果有的话(并获得"免费"动画)
我知道这个问题是在50次之前被问到的,但是我已经仔细研究了所有其他答案,但没有找到任何可以解决我问题的方法。
无论如何,这是我的代码:
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[self.cellArray removeObjectAtIndex:i];
}
}
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
Run Code Online (Sandbox Code Playgroud)
当我执行这段代码时,我得到了这个崩溃日志:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the …
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个UITableView
处于编辑模式,我强制它在包含这个tableview的viewcontroller的viewDidLoad函数上运行到编辑模式.一切都很好,每行表左边都有一个删除图标,右边是re-arrange
图标.我实施了
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
问题出在每一行我都不想在左边显示一个红色删除图标,只是在右边显示一个重新排列的图标.当用户想要删除时,他/她可以滑动一行进行删除.
我想这样做的原因是我不鼓励用户删除行产品.我明白我可以放一个编辑按钮,当用户点击这个按钮时,我会进入编辑模式,但我不想这样做,我想在用户进入我的viewcontroller时在开头显示重新排列功能.
我可以这样做吗
让我拍一些屏幕截图
第一张图是我得到的.
第二张图是我真正想要展示的.当用户滑动时,我想显示一个删除按钮,覆盖右侧的排列图标