custom editingAccessoryView not working(自定义编辑AccessoryView 不起作用)
问题描述
对于带有自定义单元格的 UITableView,我有以下代码:
I have the following code for a UITableView with custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FolderCellViewController"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FolderCellViewController" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
cell.editingAccessoryView=accessoryView; //accessoryView is a UIView within a UITableViewCell, and it is properly connected in IB
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
// 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 NO; //YES here makes a red delete button appear when I swipe
}
// 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:[NSArray arrayWithObject: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
}
}
但是对于某些人来说,当我滑动时什么都没有发生.除了这个,我什么都没做——我还需要做什么才能让它工作吗?
But for some when I swipe nothing happens. I haven't done anything but this-is there anything else I need to do for this to work?
显然我所做的只是为整个表格处于编辑模式时设置编辑样式,而不是在我在每个单独的单元格上滑动时设置编辑样式.所以我想要做的是当我在每个单元格上滑动时,该单元格的自定义附件视图会出现.但我不知道该怎么做..
Apparently what I did only sets the editing style for when the entire table is in edit mode, not when I swipe on each individual cell. So what I want to do is when I swipe on each cell, the custom accessoryView appears for that cell. But I'm not sure how to do that..
推荐答案
当单元格进入编辑模式时,显示编辑附件视图.真正让这个工作似乎有点太难了,但我已经做到了:
The editing accessory view is shown when the cell enters editing mode. It does seem a little bit too hard to actually get this working, but I have managed it:
为了让这个在进入整个表格的编辑模式时和滑动单个行时都显示出来,我在我的 UITableViewController 子类中实现了以下内容:
To get this to show both when entering edit mode for the whole table, and when swiping an individual row, I have implemented the following in my UITableViewController subclass:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
if (editing)
self.editingFromEditButton = YES;
[super setEditing:(BOOL)editing animated:(BOOL)animated];
self.editingFromEditButton = NO;
// Other code you may want at this point...
}
editingFromEditButton
是子类的 BOOL 属性.当标准的编辑"按钮被按下时,这个方法被调用.它用于以下防止标准删除按钮显示的方法:
editingFromEditButton
is a BOOL property of the subclass. This method is called when the standard "Edit" button is pressed. It is used in the following method which prevents the standard delete button showing:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editingFromEditButton)
return UITableViewCellEditingStyleNone;
// Otherwise, we are at swipe to delete
[[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES];
return UITableViewCellEditingStyleNone;
}
如果整个表格视图被设置为编辑模式,那么每个单元格也将被发送 setEditing 消息.如果我们滑动了一行,那么我们需要强制该单元格进入编辑模式,然后返回 UITableViewCellEditingStyleNone
样式以防止出现标准删除按钮.
If the whole table view is being set to editing mode then each cell will also be sent the setEditing message. If we have swiped a single row, then we need to force that cell into editing mode, and then return the UITableViewCellEditingStyleNone
style to prevent the standard delete button from appearing.
然后,要关闭自定义编辑附件,您还需要以下代码:
Then, to dismiss the custom editing accessory, you also need the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Cancel the delete button if we are in swipe to edit mode
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.editing && !self.editing)
{
[cell setEditing:NO animated:YES];
return;
}
// Your standard code for when the row really is selected...
}
这篇关于自定义编辑AccessoryView 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义编辑AccessoryView 不起作用
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01