我在表格 View 上有自定义单元格。所有自定义单元格上都有一个文本字段。文本字段以禁用状态开始。我已经对它们进行了配置,以便在长按后启用文本字段并且用户可以编辑文本字段。
但是,我希望文本字段一次只启用一个,这样用户就无法选择切换到其他单元格上的文本字段。所以我只想启用
这是我的代码
- (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];
}
}
如您所见,所有文本字段都在表格 View 中启用。如何仅使选定单元格的表格 View 处于事件状态?
谢谢。
最佳答案
您可以做的是首先隐藏 cellForRowAtIndexPath 中的所有文本字段,然后在调用长按 longPress: 方法时取消隐藏该特定单元格的文本字段,然后比较索引路径是否与长按相同,然后取消隐藏
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
---------
if(indexPath.row == selectedIndexPathRow) {
cell.textField.hidden = NO;
[cell.textField becomeFirstResponder];
}
else {
cell.textField.hidden = YES;
---------
}
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
cell = (SimpleTableCell *)[gesture view];
// get indexPath of cell
selectedIndexPath = [self.tableView indexPathForCell:cell];
selectedIndexPathRow = selectedIndexPath.row;
[self.tableView reloadData];
}
}
关于ios - 在表格 View 中仅选择一个自定义单元格的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341014/