我正在创建一个可编辑的 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/