iOS : how to enable/disable textview's editable property on click of button

标签 ios xcode uitableview textview

我正在创建一个可编辑的 TableView ,用户可以在其中将其值输入到 TableView 单元格中。 TableView 自定义单元格包含多个 TextView 。我有一个像编辑/完成的按钮。单击编辑时,用户应该能够在包含 textview 的 TableView 单元格中输入值。单击完成后,它将禁用可编辑属性。

下面是我的编辑/删除按钮方法,但它没有按我希望的那样运行,可能的解决方案是什么?

- (IBAction) toggleEdit:(id)sender {
    [self.tableview setEditing:!self.tableview.editing animated:YES];

    static NSString *simpleTableIdentifier = @"ScoreCustomCell";

    ScoreCustomCell *cell = (ScoreCustomCell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScoreCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if (self.tableview.editing) {
        NSString *tempStr=[NSString stringWithFormat:@"Done"];
        [sender setTitle:tempStr  forState:UIControlStateNormal];
        for (int i=0; i<=23; i++) {
            if (cell.scoreText.tag==i) {
                [self.tableview setUserInteractionEnabled:YES];
                [cell setUserInteractionEnabled:YES];
                cell.scoreText.editable=YES;
                [cell.scoreText setUserInteractionEnabled:YES];
                [cell.scoreText becomeFirstResponder];
                [self.tableview reloadInputViews];
            }
        }

    }
    else {
        NSString *tempStr2=[NSString stringWithFormat:@"Edit"];
        [sender setTitle:tempStr2  forState:UIControlStateNormal];
        for (int i=0; i<=23; i++) {
            if (cell.scoreText.tag==i) {
                cell.scoreText.editable=NO;
                [self.tableview reloadInputViews];

            }        }
    }
}

最佳答案

像这样更新您的 toggleEdit 方法:

- (IBAction) toggleEdit:(UIButton *)sender {
    [self.tableview setEditing:!self.tableview.editing animated:YES];

    if (self.tableview.editing) {

         NSString *tempStr = [NSString stringWithFormat:@"Done"];
        [sender setTitle:tempStr  forState:UIControlStateNormal];
    }
    else {
        NSString *tempStr2=[NSString stringWithFormat:@"Edit"];
    }

   [sender setTitle:tempStr2  forState:UIControlStateNormal];

   [self.tableView reloadData];
}

现在在 cellForRowAtIndexPath 方法的末尾放置一个 if-else

cellForRowAtIndexPath
{
   ...
   ... Your code here ...
   ...


   // before returning cell add this two lines

   [cell.scoreText setEditable:self.tableview.editing];
   [cell.scoreText setUserInteractionEnabled:self.tableview.editing];

   return cell;
}

希望这对您有所帮助。

关于iOS : how to enable/disable textview's editable property on click of button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664522/

相关文章:

ios - 为 iOS 9 启用 Bitcode 会使 IPA 大小增加 3 倍,这是 App Store 上的大小吗?

ios - 3x3 按钮的多点触控

ios - 如何在新 Storyboard后手动添加 Root View Controller 箭头?

ios - 如何在一个 View 中选择的图像在另一个 View 中显示

iphone - 带有自定义初始化数据的 xcode PushViewController

ios - UITableView 决定单个行高

swift - 禁用滚动到顶部

ios - 在代码中启动 UITalbleViewController

ios - 如何可靠地确定 Passbook 是否可用?

iOS 组件会自动调整大小,但我不知道在哪里以及为什么