【发布时间】:2014-03-12 02:46:29
【问题描述】:
我在表格视图上有自定义单元格。所有自定义单元格上都有一个文本字段。文本字段以禁用状态开始。我已经对它们进行了配置,以便在长按后启用文本字段并且用户可以编辑文本字段。
但是,我希望文本字段一次只启用一个,这样用户就无法切换到其他单元格上的文本字段。所以我只想启用
这是我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
simpleTableIdentifier = @"SimpleTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell.textField setEnabled:NO];
//put in long press
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[cell addGestureRecognizer:longPressGesture];
}
}
}
然后我们有这个:
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
cell = (SimpleTableCell *)[gesture view];
// get indexPath of cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];
// enabling text field
[cell.textField setEnabled:YES];
[cell.textField becomeFirstResponder];
}
}
如您所见,表格视图中的所有文本字段均已启用。如何仅激活所选单元格的表格视图?
谢谢。
【问题讨论】:
-
您是否考虑过将 UILongPressGestureRecognizer 添加到 SimpleTableCell.m 中单元格的 contentView 中,而不是在您的 tableView.m 中?
-
你使用 [tableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:simpleTableIdentifier]; ?
标签: ios objective-c uitableview keyboard