Amo*_*kar 75 uitableview ios uiswipegesturerecognizer
我有一个UIViewController
实现TableViews委托和数据源协议.现在我想向细胞添加"轻扫删除"手势.
我应该怎么做呢.
我给出了commitEditingStyle
方法的空白实现,并将Editing属性设置为YES.
仍然没有滑动功能.
现在我需要单独添加UISwipeGesture
到每个单元格吗?
或者我错过了什么?
Dj *_*j S 61
正如Dan上面所述,您需要实现以下表视图委托方法:
tableView:canEditRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
注意:我在iOS 6和iOS 7中尝试过这个.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform the real delete action here. Note: you may need to check editing style
// if you do not perform delete only.
NSLog(@"Deleted row.");
}
Run Code Online (Sandbox Code Playgroud)
Kyr*_*off 53
editing:YES
如果需要在单元格滑动上显示"删除"按钮,则无需进行设置.您必须实现tableView:canEditRowAtIndexPath:
并从那里返回YES,以便您需要编辑/删除行.当tableView的dataSource是UITableViewContoller的子类时,这不是必需的 - 如果不覆盖此方法,则默认情况下返回YES.在所有其他情况下,您必须实现它.
编辑:我们一起发现了问题- tableView:editingStyleForRowAtIndexPath:
返回UITableViewCellEditingStyleNone
如果表中编辑模式没有.
Can*_*soy 25
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Run Code Online (Sandbox Code Playgroud)
Mas*_*iYa 13
请在swift中尝试此代码,
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// let the controller to know that able to edit tableView's row
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in
// Your delete code here.....
.........
.........
})
// You can set its properties like normal button
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
}
Run Code Online (Sandbox Code Playgroud)
尝试将以下内容添加到您的班级:
// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return(YES);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
83153 次 |
最近记录: |