Ios 自定义编辑附件视图不工作
对于带有自定义单元格的UITableView,我有以下代码:Ios 自定义编辑附件视图不工作,ios,uitableview,uikit,Ios,Uitableview,Uikit,对于带有自定义单元格的UITableView,我有以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FolderCellViewController"]; if (cel
- (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
}
}
但对一些人来说,当我刷卡时,什么也没发生。我什么都没做,但这还需要我做什么才能让它工作吗
编辑:显然,我所做的只是在整个表格处于编辑模式时设置编辑样式,而不是在每个单元格上滑动时设置编辑样式。因此,我要做的是,当我在每个单元格上滑动时,该单元格将显示自定义的accessoryView。但我不知道如何做到这一点。当单元格进入编辑模式时,会显示编辑附件视图。要让它真正发挥作用似乎有点太难了,但我已经做到了: 为了在为整个表进入编辑模式时以及在扫描单个行时都显示这一点,我在UITableViewController子类中实现了以下功能:
- (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属性。当按下标准的“编辑”按钮时,调用此方法。它用于以下方法,防止标准删除按钮显示:
- (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
样式以防止出现标准的删除按钮
然后,要关闭自定义编辑附件,还需要以下代码:
- (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...
}
如果更改
[[tableView cellForRowAtIndexPath:indexPath]设置编辑:是动画:是]
至[[tableView cellForRowAtIndexPath:indexPath]设置编辑:!cell.isEditing动画:是]
您可以获得额外的“刷消”奖励,其方式与默认的“删除”按钮相同。