Shaunak
Shaunak

Reputation: 717

Create editable UITextView inside a UITableViewController cell

I created a uitextview inside a uitableviewcontroller programatically but i am unable to edit the textview. Here's an overview to my table layout:

Row1: UILabel

Row2: Uneditable UITextview

Row3: UILabel

Row4: Editable UITextview

Here's what i am doing:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
UILabel *label = nil;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(indexPath.row%2==0)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 50)];
    [label setBackgroundColor:[self.cellBackgroundColor objectAtIndex:indexPath.row]];
    [label setText:[self.celltitle objectAtIndex:indexPath.row]];
    label.font=[UIFont fontWithName:[self.cellFontName objectAtIndex:indexPath.row] size:[[self.cellFontSize objectAtIndex:indexPath.row] integerValue]];

    label.textAlignment = NSTextAlignmentLeft;
    [[cell contentView] addSubview:label];
}
else{

    UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 150)];
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.userInteractionEnabled=YES;
    if(indexPath.row==3)
    {
       [messageBox setEditable:YES];
[messageBox setUserInteractionEnabled:YES];
        messageBox.delegate=self;
        messageBox.editable=YES;
    }
     [cell.contentView addSubview: messageBox];


}
return cell;}

I have also set textviewdelegate in header file and textviewshouldbeginediting method but row4 textview is still not editable... Any help?

Upvotes: 0

Views: 3863

Answers (4)

Sudha Tiwari
Sudha Tiwari

Reputation: 2451

Try this.. May be it will help you..Its working for me

 if(indexPath.row==3)
    {
        messageBox.delegate=self;
       [messageBox setEditable:YES];
       [messageBox setUserInteractionEnabled:YES];
        messageBox.editable=YES;
        [cell addSubview: messageBox];
    }

Upvotes: 1

Anil Varghese
Anil Varghese

Reputation: 42977

I tried your code..Its working for me.. see the changes bellow

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UILabel *label = nil;
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (nil == cell)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    if(indexPath.row%2==0)
    {
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 50)];
          [label setBackgroundColor:[self.cellBackgroundColor objectAtIndex:indexPath.row]];
         [label setText:[self.celltitle objectAtIndex:indexPath.row]];
         label.font=[UIFont fontWithName:[self.cellFontName objectAtIndex:indexPath.row] size:[[self.cellFontSize objectAtIndex:indexPath.row] integerValue]];

        label.textAlignment = NSTextAlignmentLeft;
        [[cell contentView] addSubview:label];
    }
    else{

        UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 100)];

        cell.userInteractionEnabled=YES;
        if(indexPath.row==1)
        {
            // Uneditable UITextview
            messageBox.editable=NO;
        }
        [cell.contentView addSubview: messageBox];

    }
}
return cell;
 }

Upvotes: 0

luksfarris
luksfarris

Reputation: 1561

You have to initialize the cell´s content view before using it. Try this:

cell.contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 150)];

Upvotes: -1

Albert Renshaw
Albert Renshaw

Reputation: 17892

In addition to making the messageBox setEditable and setUserInteractionEnabled, you ALSO have to make sure those properties are enabled in your UITableViewController as well since the UITextView is nested within it!

[tableView setUserInteractionEnabled:YES];
[cell setUserInteractionEnabled:YES];
[messageBox setEditable:YES];
[messageBox setUserInteractionEnabled:YES];

(*Note, your tableView and TableViewCell both have the same name from this function so I would put that code elsewhere but I added it above just incase)

Upvotes: 2

Related Questions