【发布时间】:2010-04-30 13:24:18
【问题描述】:
我可以通过调用 [tableView setEditing:YES] 来设置一段时间 uitableview 的编辑模式;但这会为表中的所有行设置编辑模式。
有没有办法检测哪一行被刷过并只为该行启用编辑模式?
谢谢
【问题讨论】:
标签: iphone uitableview
我可以通过调用 [tableView setEditing:YES] 来设置一段时间 uitableview 的编辑模式;但这会为表中的所有行设置编辑模式。
有没有办法检测哪一行被刷过并只为该行启用编辑模式?
谢谢
【问题讨论】:
标签: iphone uitableview
我还没有编写代码,但这是我的想法。
在 .h 中创建 selectionIndexPath
NSIndexPath *selectionIndexPath;
然后在.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
将 indexPath 保存到 selectionIndexPath 并调用:
[self.tableView setEditing:YES];
然后在:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
if (selectionPath.row == indexPath.row)
{
return UITableViewCellEditingStyleDelete;
}
else
{
return UITableViewCellEditingStyleNone;
}
}
您也可以捕捉到触摸,然后或多或少地做同样的事情。像这样的……
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
// save indexPath and setEditing Mode here
}
没有时间编写代码,但这是主要思想。
【讨论】:
实现editingStyleForRowAtIndexPath方法:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 3)
{
return UITableViewCellEditingStyleDelete;
}
else
{
return UITableViewCellEditingStyleNone;
}
}
//
//
编辑:正如您在 cmets 中阐明的那样,您希望让用户在单元格上滑动,然后删除或移动单元格。
我相信让用户两者都做,表格(不仅仅是单元格)必须置于编辑模式,如果你只在用户滑动单元格时这样做,表格重绘时会出现烦人的闪烁本身为插入/删除按钮腾出空间。 Jordan 提供了执行此操作的示例代码。
Ian Henry 建议的替代方法是实现滑动删除。但是,似乎没有滑动移动等效项。要实现滑动删除,请执行以下操作:
editingStyleForRowAtIndexPath 并且只实现 return UITableViewCellEditingStyleDelete 而不管索引路径如何willBeginEditingRowAtIndexPath,但什么也不做commitEditingStyle 并在其中从您的数据源中删除行并调用deleteRowsAtIndexPaths
现在,当用户滑动单元格时,右侧会出现一个删除按钮。如果用户点击它,commitEditingStyle 将被调用,如果用户点击其他任何东西,删除将被取消。
【讨论】:
UITableViewCell 也有一个方法-setEditing:animated:。因此,如果您在自定义 UITableViewCell 中覆盖它并仅在消息可编辑时将消息发送给 super,您就可以实现您想要的。
为了更清楚。子类 UITableViewCell 并在其中维护一个布尔值,例如:
@interface CustomTableViewCell : UITableViewCell
{
BOOL cellEditable;
}
@property (readwrite, assign) BOOL cellEditable;
@end
接下来,在您的 tableview 委托方法中,返回一个 CustomTabeViewCell 对象,并为该行适当设置了 cellEditable 属性(该行是否可编辑)。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *newCell = [[CustomTableViewCell alloc] init];
if (row_is_editable)
[newCell setCellEditable:YES];
else
[newCell setCellEditable:NO];
return [newCell autorelease];
}
根据UITableViewCell的- (void)setEditing:(BOOL)editing animated:(BOOL)animated方法的文档——
"当你在editing的值设置为YES的情况下调用这个方法,并且UITableViewCell对象被配置为有控件时,单元格会在每个左侧显示一个插入(绿色加号)或删除控件(红色减号)单元格和右侧的重新排序控件。当调用 UITableView 的 setEditing:animated: 方法时,在每个可见单元格上调用此方法。在编辑设置为 NO 的情况下调用此方法会从单元格中删除控件。"
所以我们需要做的就是覆盖 CustomTableViewCell 中的 -setEditing:animated: 方法并执行以下操作:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
if ([self cellEditable])
{
[super setEditing:editing
animated:animated];
}
else
{
[super setEditing:NO
animated:NO];
}
}
您已经完成了,现在当您在表格视图上触发-setEditing:animated: 时,只有您设置为 cellEditable 的单元格的行才能被编辑。
【讨论】: