如何根据ios

时间:2015-07-21 12:05:05

标签: ios objective-c

您好我是Ios的初学者在我的项目中我已经在UItableview单元格上添加了UItextView并且基于textlength自动UItextview和UItableview单元格高度需要增加但是根据我的代码所有textdata都没有显示在textview上有些数据丢失请帮帮我一个人

这是我的代码:

 formulaArray = [NSArray arrayWithObjects:@"Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry,finally",nil];


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

if(indexPath.section == 1)
    {

        formulaText = [[UITextView alloc]init];
        formulaText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        formulaText.translatesAutoresizingMaskIntoConstraints = NO;
        formulaText.backgroundColor = [UIColor lightGrayColor];
        formulaText.scrollEnabled = NO;
        [cell.contentView addSubview:formulaText];

        NSDictionary * views = NSDictionaryOfVariableBindings(formulaText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[formulaText]-10-|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[formulaText]-5-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        formulaText.text = [formulaArray objectAtIndex:0];

        #define kMaxHeight 100.f
        formulaText.contentSize = [formulaText.text sizeWithFont:[UIFont fontWithName:@"Bitter-Regular" size:15.0]
                                                   constrainedToSize:CGSizeMake(100, kMaxHeight)
                                                       lineBreakMode:NSLineBreakByWordWrapping];

    }

    if (indexPath.section == 2)
    {
        aboutText = [[UITextView alloc]init];
        aboutText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        aboutText.translatesAutoresizingMaskIntoConstraints = NO;
        aboutText.backgroundColor = [UIColor darkGrayColor];
        aboutText.scrollEnabled = NO;
        [cell.contentView addSubview:aboutText];

        NSDictionary * views = NSDictionaryOfVariableBindings(aboutText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[aboutText]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[aboutText]-5-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        aboutText.text = [aboutArray objectAtIndex:0];
        #define kMaxHeight 100.f
        aboutText.contentSize = [aboutText.text sizeWithFont:[UIFont fontWithName:@"Bitter-Regular" size:15.0]
                                               constrainedToSize:CGSizeMake(100, kMaxHeight)
                                               lineBreakMode:NSLineBreakByWordWrapping];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //return 200;

    if([indexPath section] == 0)
    {
        return  200;
    }

    else if([indexPath section] == 1)
    {
        NSString *cellText = [formulaArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }

    else
    {
        NSString *cellText = [aboutArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }
}

但是这里公式数组所有文本enter image description here数据都没有显示请帮帮我一些

3 个答案:

答案 0 :(得分:1)

试试这个。

  - (CGSize )getLabelSizeForString:(NSString *)message      withLabel:(UILabel *)label{
     CGSize size = [message sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping];
      return size;
   }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSString *message = [[self.dataArray objectAtIndex:indexPath.row]valueForKey:Message];
CGSize size = [[GlobalConstants sharedInstance] getLabelSizeForString:message withLabel:cell.aboutLabel];

    return size.height;
   }

或者你可以提供链接 http://www.fantageek.com/1468/ios-dynamic-table-view-cells-with-varying-row-height-and-autolayout/

答案 1 :(得分:0)

我认为这个功能

CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

在您的身高ForRowAtIndexPath中弃用。

尝试使用NSFontAttribute代替它。在你的情况下,它看起来像这样:

CGRect textRect = [cellText boundingRectWithSize:constraintSize
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:cellFont}
                                 context:nil];
CGSize labelSize = textRect.size;

如果您想动态增加它,则需要刷新已编辑的特定单元格。在你的

- (void)textViewDidChange:(UITextView *)textView

您需要刷新您的手机。

  [tableView reloadRowsAtIndexPaths:@[yourIndex] withRowAnimation:UITableViewRowAnimationNone];

答案 2 :(得分:0)

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return formulaArray.count;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];


    cell.myTextView.text     = formulaArray[indexPath.row];

    [cell.contentView layoutIfNeeded];
    return cell;
}

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

您可以通过故事板轻松实现这些功能。

  1. 在单元格中拖动textview并仅添加top,bottom,leading和trailing约束。

  2. 取消选中为textview启用滚动功能。

  3. 然后实施这些方法