[英]UITableView Edit mode
我有UITableView
,我試圖在編輯模式下默認加載它。 問題是當我這行table.editing=TRUE;
我的行消失了,我實現了這個方法:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
但沒有運氣。 我該怎么辦?
正如Anish所指出的那樣
[tableView setEditing: YES animated: YES];
但是你需要在viewWillAppear
視圖事件中使它才能使它工作。
試試這個...
[tableView setEditing: YES animated: YES];
在ViewDidLoad中寫
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];
addORDoneRow
- (void)addORDoneRows
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[_dbSongsTblView setEditing:NO animated:NO];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[_dbSongsTblView setEditing:YES animated:YES];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
對於Row的MultipleSelection
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
注意:有三種編輯風格如下
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
注意:對於多個選擇集,從屬性檢查器中選擇和編輯樣式為多個
要在編輯模式下加載tableView,您應該在viewDidLoad()
調用setEditing(true, animated: false)
viewDidLoad()
。
如果您的視圖控制器是UITableViewController
的子類,則無需更改,只需進行上述調用即可。 否則,如果您的視圖控制器是UIViewController的子類,那么您應該以這種方式進行調用: tableView.setEditing(true, animated: true)
。
用Swift 2.2測試。
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.