【问题标题】:UITextView in a grouped UITableView's cellUITextView 在分组 UITableView 的单元格中
【发布时间】:2012-07-24 15:42:37
【问题描述】:

请解释一下:如何以编程方式在 UITableViewCell 中创建 UITextView(UITableView 具有分组样式)。文本视图大小应等于单元格的大小。

UITextView 的文本和边框(左上角)之间存在间隙,因此我需要正确的坐标才能将文本视图正确放置在单元格上。

更新:我已经解决了我的问题。请参阅下面的我的自我回答。谢谢!

【问题讨论】:

  • 您最好的选择可能是在您的问题中显示您当前的代码,以便人们提出改进建议。
  • 创建UITextView对象的实例并将其作为子视图添加到您的UITableCellView中,并确保在重用任何旧UITableCell时,不要添加@987654324的新实例如果您不想不必要地浪费内存,请再次 @ 到同一单元格。
  • 为了更清楚,我已经编辑了问题。
  • 要将您的问题标记为已解决,请接受答案,不要更新问题的标题。

标签: objective-c ios uitableview uitextview


【解决方案1】:

很简单:

    textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);

【讨论】:

    【解决方案2】:

    给你:

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];
        UITextField *textfield;
        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                          reuseIdentifier:@"Prototype Cell"];
            textfield = [[UITextField alloc] init];
            [cell.contentView addSubview:textfield];
            textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
        }
        else 
        {
            textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
            textfield.frame = cell.frame;
        }
        return cell;
    }
    

    【讨论】:

    • 我想创建一个 UITextView 对象,而不是 UITextField。 UITextView 在其文本和边框(左上角)之间存在间隙,因此我需要正确的坐标才能将文本视图正确放置在单元格上。
    【解决方案3】:

    设置 UITextView 的“contentInset”

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *sCellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier]; 如果(!单元格) { 单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail 重用标识符:sCellIdentifier]; } UITextView *textView = [UITextView alloc]init]; textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //根据你的要求改变 textView.frame = 单元格.frame; [textView setUserInteractionEnabled:FALSE]; [textView setBackgroundColor:[UIColor clearColor]]; [cell.contentView addSubView:textView]; 返回单元格; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2013-02-19
      相关资源
      最近更新 更多