2012-07-24 61 views
1

請解釋我:如何以編程方式在UITableViewCell中創建UITextView(UITableView具有分組樣式)。文本視圖大小應該等於單元格的大小。UITextView在分組的UITableView的單元

UITextView在其文本和邊框(左上角)之間存在間隙,所以我需要正確的座標以正確放置文本視圖到單元格上。

UPDATE:我解決了我的問題。請參閱下面的自我回答。謝謝!

+1

您最好的選擇可能將顯示您當前的代碼你的問題,以便人們可以建議改進。 – 2012-07-24 15:49:08

+0

創建'UITextView'對象的實例並將其作爲子視圖添加到您的'UITableCellView'中,並且確保何時重用任何舊的'UITableCell',如果您再次添加新的'UITextView'實例到同一個單元格不想浪費內存不必要的。 – holex 2012-07-24 15:50:08

+0

爲了更加清晰,我編輯了這個問題。 – rmxmaster 2012-07-24 16:40:21

回答

1

很簡單:

textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height); 
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; 
} 
+0

我想創建一個UITextView對象,而不是UITextField。 UITextView在其文本和邊框(左上角)之間有空隙,所以我需要正確的座標以正確放置文本視圖到單元格上。 – rmxmaster 2012-07-24 16:34:45

0

集 「contentInset」 的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; 
}