iOSNoob
iOSNoob

Reputation: 1420

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

I am creating a editable table view where user can enter its values into tableview cells. Table view custom cells contains multiple textview. I have a button like Edit/Done. On click of edit, user should be able to enter values in table view cells containing textview. On Done click, it will disable editable property.

below is my method of Edit/Delete button, but its not functioning as I am wishing to, what will be the possible solution ?

- (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];

            }        }
    }
}

Upvotes: 2

Views: 834

Answers (1)

Mrunal
Mrunal

Reputation: 14118

Update your toggleEdit method like this:

- (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];
}

Now in cellForRowAtIndexPath method at the end put an 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;
}

Hope this helps.

Upvotes: 1

Related Questions