我有一个自定义 UITableViewCell,上面有 1 个文本字段和一个标签。我想仅在表格 View 处于编辑模式时启用文本字段编辑。
我使用以下方法来启用文本字段编辑模式和禁用文本字段编辑模式。但这不起作用。我不确定这是否是正确的方法。如果这不是正确的方法,你能让我知道如何禁用启用文本字段吗?
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *rowToSelect = indexPath;
EditingTableViewCell *detSelCell;
detSelCell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
detSelCell.textField.enabled = self.editing;
// Only allow selection if editing.
if (self.editing)
{
return indexPath;
}
else
{
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.editing)
{
return;
}
EditingTableViewCell *detcell;
detcell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
detcell.selectionStyle = style;
detcell.textField.enabled = self.editing;
我还有以下几行:
self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.
请帮忙!
最佳答案
---我找到了答案:
我已从以下方法中删除了启用/禁用代码:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
并在自定义 cell.m 中添加以下内容
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
if(editing){
textField.enabled = YES;
}else{
textField.enabled = NO;
}
[UIView commitAnimations];
}
它现在正在工作。我不确定这是否是正确的方法,但它工作正常。
关于iphone - 在 UITableViewCell 中启用/禁用编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9649986/