rmxmaster
rmxmaster

Reputation: 156

UITextView in a grouped UITableView's cell

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.

The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.

UPDATE: I've solved my question. See my self-answer below. Thanks!

Upvotes: 1

Views: 1620

Answers (3)

rmxmaster
rmxmaster

Reputation: 156

It was simple:

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

Upvotes: 1

spaleja
spaleja

Reputation: 1435

Set "contentInset" of UITextView

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

Upvotes: 0

Nosrettap
Nosrettap

Reputation: 11320

Here you go:

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

Upvotes: 0

Related Questions