在UITableViewController单元格中创建可编辑的UITextView

时间:2013-02-14 13:07:02

标签: ios uitextview uitableview

我以编程方式在uitableviewcontroller中创建了一个uitextview但我无法编辑textview。这是我的表格布局的概述:

第1行:UILabel

第2行:不可编辑的UITextview

第3行:UILabel

第4行:可编辑的UITextview

这就是我在做的事情:

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

我还在头文件和textviewshouldbeginediting方法中设置了textviewdelegate但是row4 textview仍然不可编辑...有什么帮助?

4 个答案:

答案 0 :(得分:2)

除了制作messageBox setEditable和setUserInteractionEnabled之外,你还必须确保在你的UITableViewController中启用了这些属性,因为UITextView嵌套在它里面!

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

(*注意,你的tableView和TableViewCell都有这个函数的相同名称,所以我会把那些代码放在别处,但我只是在上面加了它)

答案 1 :(得分:1)

试试这个..可能它会帮助你......它为我工作

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

答案 2 :(得分:0)

我试过你的代码......它为我工作..看下面的变化

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

答案 3 :(得分:-1)

在使用之前,您必须初始化单元格的内容视图。试试这个:

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