【问题标题】:Create editable UITextView inside a UITableViewController cell在 UITableViewController 单元格中创建可编辑的 UITextView
【发布时间】:2013-02-14 13:07:02
【问题描述】:

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

第 1 行:UILabel

Row2:不可编辑的 UITextview

第 3 行:UILabel

Row4:可编辑的 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;}

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

【问题讨论】:

  • 委托方法有响应吗?
  • No.. 委托方法没有响应..
  • 你的 .h 中是否有正确的链接并且你已经添加了 ?
  • 是的,我已经检查了所有内容都已添加...
  • 或其他问题可能是您已经将 seteditable 设置为 yes 但显然某些视图与您的 textview 重叠。尝试为所有视图设置不同的背景颜色,然后查看 textview 在单元格上的位置。

标签: ios uitextview uitableview


【解决方案1】:

除了设置 messageBox 和 setUserInteractionEnabled 之外,您还必须确保在 UITableViewController 中启用这些属性,因为 UITextView 嵌套在其中!

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

(*注意,您的 tableView 和 TableViewCell 在此函数中都具有相同的名称,因此我将该代码放在其他地方,但我将其添加到上面以防万一)

【讨论】:

    【解决方案2】:

    试试这个..也许它会帮助你..它对我有用

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

    【讨论】:

    • [单元格 addSubview: messageBox];将此行写在括号中
    【解决方案3】:

    我试过你的代码..它对我有用..看看下面的变化

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多