Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 嵌入在UITableViewCell中的UITextView未正确调整大小_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 嵌入在UITableViewCell中的UITextView未正确调整大小

Ios 嵌入在UITableViewCell中的UITextView未正确调整大小,ios,objective-c,uitableview,Ios,Objective C,Uitableview,以下是我在cellForRowAtIndexPath委托中的代码: Status *status = nil; status = [myArray objectAtIndex:indexPath.row]; NSString* postvalue = status.statusContent; cell.postContent.scrollEnabled = NO; CGSize mysize = [postvalue sizeWithAttributes:@{NSFontAttributeN

以下是我在cellForRowAtIndexPath委托中的代码:

Status *status = nil;
status = [myArray objectAtIndex:indexPath.row];
NSString* postvalue = status.statusContent;
cell.postContent.scrollEnabled = NO;

CGSize mysize = [postvalue sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}];
CGRect frame = cell.postContent.frame;
frame.size.height = mysize.height;
cell.postContent.frame = frame;

cell.postContent.text = postvalue;
其中cell.postContent是文本视图

下面是我调整单元格大小的代码:

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
   Status *status = nil;
   status = [myArray objectAtIndex:indexPath.row];
   NSString* postvalue = status.statusContent;

   return 100 + [self heightForText:postvalue];
}


-(CGFloat)heightForText:(NSString *)text
{
   NSInteger MAX_HEIGHT = 2000;
   UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 226, MAX_HEIGHT)];
   textView.text = text;
   textView.font = [UIFont systemFontOfSize:17.0f];// your font
   [textView sizeToFit];

   return textView.frame.size.height;
}
这就是它的结果:

我已将用户名替换为cellForRow中计算的高度。。委托,以便您可以看到高度计算正确,单元格大小调整正确,但textview不正确-有很多行是您看不到的。有什么想法吗


谢谢,这是因为您不应该创建新的文本字段(您也忘了将其添加为子视图),而应该调整单元格出口的大小。创建单元格时,尝试在cellForRow中设置textField的高度

UITextView
在其周围使用一些默认填充,因此这可能是个问题。按代码设置
UITextView
contentInset

// this is how we define setContentInset method
[textView setContentInset:UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)];

// and you should do like this
[textView setContentInset:UIEdgeInsetsMake(-8, -8, -8 ,-8)];

你可以访问它的内容视图我想,这是代码, 我正在使用一些值来测试它是否正常工作,通过这个希望可以帮助你…:)

禁用文本字段的自动布局


在CustomCelll.h文件中

   @interface CustomCelll : UITableViewCell
   //i think u hav an outlet for textview
   @property (retain, nonatomic) IBOutlet UITextView *postContent;
   @property (nonatomic,retain)NSString *message;//you get the message to set the height

   in CustomCelll.m file

   @implementation CustomCelll
   @synthesize postContent;
   @synthesize imageView;
   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
      //heare i am adding the button
        UIFont *font = [UIFont systemFontOfSize:17.0f];
        self.postContent.frame = CGRectZero;//initially set it zero
        self.postContent.font = font;
        }
      return self;

  }

   - (void)layoutSubviews
    {
     [super layoutSubviews];
     CGSize size = [self findMessgeStringHeight];//you can ge the size
     //set frame for ur label
     self.postContent.frame = CGRectMake(103, 8, size.width + 10,size.height + 10);//set the height here(for your case) and disable auto layout  
     self.postContent.text = self.message;//set the message
   }

  - (CGSize)findMessgeStringHeight
   {

         NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.message attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f] }];
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
        CGSize requiredSize = rect.size;

       return requiredSize; //finally u return your height
  }


   //in your controller

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
    CustomCelll *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
     }
   cell.message = @"hello, how are u, it been so long since we met, how's the weather there, here too cold, i am going to my mom's home may be i will stay there for 2 or 3 weeks";
   return cell;
   }

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {

      NSString *str = @"hello, how are u, it been so long since we met, how's the weather there, here too cold, i am going to my mom's home may be i will stay there for 2 or 3 weeks";//use same  sring to get the height

     NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:str attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f]
 }];
     CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
     CGSize requiredSize = rect.size;
     return requiredSize.height + 30;
   }


尝试在子类单元格本身中设置框架…我无法从那里访问单元格内容@shan您可以在-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)中返回适当的高度(文本视图的高度+填充)检查我发布的答案..我现在编辑了我的答案检查我已经在做这个了。。。CGSize mysize=[postvalue sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}];CGRect frame=cell.postContent.frame;frame.size.height=mysize.height;cell.postContent.frame=框架;cell.postContent是否不推荐OutletiSizeWithFont?另外,我不知道这与我正在做的有什么不同,还有,为什么你要将textView添加为子视图而不是后内容?是的,这是因为没有arc你可以更改它,我没有发布它,等等,我编辑我的答案,你是否使用自定义单元格的xib文件。。。?beck我是通过编程来完成的是的,我使用的是xib,所以我不需要通过编程来创建子视图