UITextView在分组的UITableView单元格中

时间:2012-07-24 15:42:37

标签: objective-c ios uitableview uitextview

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

UITextView在文本和边框(左上角)之间有一个间隙,所以我需要正确的坐标才能正确地将文本视图放在单元格上。

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

3 个答案:

答案 0 :(得分:1)

很简单:

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

答案 1 :(得分:0)

你走了:

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

答案 2 :(得分:0)

设置UITextView的“contentInset”

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *sCellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
                                      reuseIdentifier:sCellIdentifier];
    }
    UITextView *textView = [UITextView alloc]init];
    textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
    textView.frame = cell.frame;
    [textView setUserInteractionEnabled:FALSE];
    [textView setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubView:textView];
    return cell;
}