请解释我:如何以编程方式在UITableViewCell中创建UITextView(UITableView具有分组样式)。文本视图大小应该等于单元格的大小。UITextView在分组的UITableView的单元
UITextView在其文本和边框(左上角)之间存在间隙,所以我需要正确的坐标以正确放置文本视图到单元格上。
UPDATE:我解决了我的问题。请参阅下面的自我回答。谢谢!
请解释我:如何以编程方式在UITableViewCell中创建UITextView(UITableView具有分组样式)。文本视图大小应该等于单元格的大小。UITextView在分组的UITableView的单元
UITextView在其文本和边框(左上角)之间存在间隙,所以我需要正确的坐标以正确放置文本视图到单元格上。
UPDATE:我解决了我的问题。请参阅下面的自我回答。谢谢!
很简单:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
在这里你去:
- (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在其文本和边框(左上角)之间有空隙,所以我需要正确的坐标以正确放置文本视图到单元格上。 – rmxmaster 2012-07-24 16:34:45
集 “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; }
您最好的选择可能将显示您当前的代码你的问题,以便人们可以建议改进。 – 2012-07-24 15:49:08
创建'UITextView'对象的实例并将其作为子视图添加到您的'UITableCellView'中,并且确保何时重用任何旧的'UITableCell',如果您再次添加新的'UITextView'实例到同一个单元格不想浪费内存不必要的。 – holex 2012-07-24 15:50:08
为了更加清晰,我编辑了这个问题。 – rmxmaster 2012-07-24 16:40:21