自定义编辑AccessoryView 不起作用
我有以下带有自定义单元格的 UITableView 代码:
- (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
}
}
但是对于某些情况,当我滑动时什么也没有发生。除了这个我什么都没做——我还需要做其他什么才能让它发挥作用吗?
编辑:显然,我所做的只是设置整个表格处于编辑模式时的编辑样式,而不是当我在每个单独的单元格上滑动时的编辑样式。所以我想做的是,当我在每个单元格上滑动时,会出现该单元格的自定义配件视图。但我不知道该怎么做..
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?
EDIT: 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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当单元格进入编辑模式时,将显示编辑附件视图。要真正实现这个工作似乎有点太难了,但我已经做到了:
为了让这个在进入整个表的编辑模式时以及在滑动单个行时都显示,我在我的 UITableViewController 中实现了以下内容子类:
editingFromEditButton
是子类的 BOOL 属性。当按下标准“编辑”按钮时将调用此方法。它在以下方法中使用,可防止显示标准删除按钮:如果将整个表视图设置为编辑模式,则每个单元格也将收到 setEditing 消息。如果我们滑动了单行,那么我们需要强制该单元格进入编辑模式,然后返回 UITableViewCellEditingStyleNone 样式以防止出现标准删除按钮。
然后,要关闭自定义编辑附件,您还需要以下代码:
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:
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:
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: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: