请解释一下:如何以编程方式在 UITableViewCell 中创建 UITextView(UITableView 具有分组样式)。文本视图大小应等于单元格的大小。
UITextView 在其文本和边框(左上角)之间存在间隙,因此我需要正确的坐标才能将文本视图正确放置在单元格上。
更新:我已经解决了我的问题。请参阅下面的我的自我回答。谢谢!
请解释一下:如何以编程方式在 UITableViewCell 中创建 UITextView(UITableView 具有分组样式)。文本视图大小应等于单元格的大小。
UITextView 在其文本和边框(左上角)之间存在间隙,因此我需要正确的坐标才能将文本视图正确放置在单元格上。
更新:我已经解决了我的问题。请参阅下面的我的自我回答。谢谢!
这很简单:
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 的“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]; 返回单元格; }