<i id='86RtR'><tr id='86RtR'><dt id='86RtR'><q id='86RtR'><span id='86RtR'><b id='86RtR'><form id='86RtR'><ins id='86RtR'></ins><ul id='86RtR'></ul><sub id='86RtR'></sub></form><legend id='86RtR'></legend><bdo id='86RtR'><pre id='86RtR'><center id='86RtR'></center></pre></bdo></b><th id='86RtR'></th></span></q></dt></tr></i><div id='86RtR'><tfoot id='86RtR'></tfoot><dl id='86RtR'><fieldset id='86RtR'></fieldset></dl></div>

      <small id='86RtR'></small><noframes id='86RtR'>

        <legend id='86RtR'><style id='86RtR'><dir id='86RtR'><q id='86RtR'></q></dir></style></legend>
        <tfoot id='86RtR'></tfoot>

          <bdo id='86RtR'></bdo><ul id='86RtR'></ul>

        IOS:具有自定义 uitableviewcell 的动态高度

        IOS: dynamic height with a custom uitableviewcell(IOS:具有自定义 uitableviewcell 的动态高度)
        <i id='9nURW'><tr id='9nURW'><dt id='9nURW'><q id='9nURW'><span id='9nURW'><b id='9nURW'><form id='9nURW'><ins id='9nURW'></ins><ul id='9nURW'></ul><sub id='9nURW'></sub></form><legend id='9nURW'></legend><bdo id='9nURW'><pre id='9nURW'><center id='9nURW'></center></pre></bdo></b><th id='9nURW'></th></span></q></dt></tr></i><div id='9nURW'><tfoot id='9nURW'></tfoot><dl id='9nURW'><fieldset id='9nURW'></fieldset></dl></div>
        1. <small id='9nURW'></small><noframes id='9nURW'>

                <tbody id='9nURW'></tbody>

                <tfoot id='9nURW'></tfoot>

                <legend id='9nURW'><style id='9nURW'><dir id='9nURW'><q id='9nURW'></q></dir></style></legend>
                • <bdo id='9nURW'></bdo><ul id='9nURW'></ul>
                  本文介绍了IOS:具有自定义 uitableviewcell 的动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 ios 开发的新手,到目前为止这是我的问题.我能够通过委托heightForRowAtIndexPath"动态计算自定义单元格的高度.所以在第一次加载时,一切都显示得很好.

                  I am new at ios development and this is my problem thus far. I am able to dynamically calculate the height of a custom cell via the delegate "heightForRowAtIndexPath". So on the first load, every thing is display perfectly fine.

                  我的问题是,一旦我开始滚动,一切都变得一团糟.我认为在滚动heightForRowAtIndexPath"时不再调用每个显示在屏幕上的单元格,因此无法计算新的单元格高度.

                  My problem is as soon as I start scrolling, everything just messes up. I think that while scrolling the "heightForRowAtIndexPath" is no longer called per cell that get display on screen, so the new cell height cannot be calculated.

                  所以我在这里被困了一段时间.我想知道你们中是否有人可以帮我一把.提前谢谢你.

                  So I've been stuck here for a while. I was wondering if any of you could lend me a hand. thank you in advance.

                  我会将代码发布到相关文件中.这些文件包括自定义单元格 h 和 m 文件.以及视图控制器m文件的相关功能.

                  I'll post the code to relevant files. These files include the custom cell h and m file. and relevant functions of the view controller m file.

                  // ######################################################
                  // HelpTableViewCell.h
                  #import <UIKit/UIKit.h>
                  
                  @interface HelpTableViewCell : UITableViewCell  {
                    IBOutlet UILabel *labelName;
                    IBOutlet UILabel *labelDescription;
                  
                    IBOutlet UIView *viewBackground;
                  }
                  
                  @property (nonatomic, retain) UILabel *labelName;
                  @property (nonatomic, retain) UILabel *labelDescription;
                  
                  @property (nonatomic, retain) UIView *viewBackground;
                  
                  @end
                  
                  // ######################################################
                  // HelpTableViewCell.m
                  #import "HelpTableViewCell.h"
                  
                  @implementation HelpTableViewCell
                  
                  @synthesize labelName;
                  @synthesize labelDescription;
                  
                  @synthesize viewBackground;
                  
                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
                    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
                  
                    if (self) {
                    }
                  
                    return self;
                  }
                  
                  - (void)setSelected:(BOOL)selected animated:(BOOL)animated  {
                  
                    self.labelName.lineBreakMode = UILineBreakModeWordWrap;
                    self.labelName.numberOfLines = 0;
                    self.labelName.font = [UIFont boldSystemFontOfSize:15];
                    [self.labelName sizeToFit];
                  
                    self.labelDescription.lineBreakMode = UILineBreakModeWordWrap;
                    self.labelDescription.numberOfLines = 0;
                    self.labelDescription.font = [UIFont fontWithName:@"Arial" size:15];
                    [self.labelDescription sizeToFit];
                  
                    [super setSelected:selected animated:animated];
                  }
                  
                  - (void) dealloc  {
                    [labelName release], labelName = nil;
                    [labelDescription release], labelDescription = nil;
                  
                    [super dealloc];
                  }
                  
                  @end
                  
                  
                  // ######################################################
                  // in my view controller m file
                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {  
                    static NSString *CellIdentifier = @"HelpTableViewCell";
                    HelpTableViewCell *cell = (HelpTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                  
                    if (cell == nil)  {
                      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HelpTableViewCell" owner:nil options:nil];
                  
                      for (id currentObject in topLevelObjects) {
                        if ([currentObject isKindOfClass:[UITableViewCell class]])  {
                          cell = (HelpTableViewCell *) currentObject;
                          break;
                        }
                      }
                  
                    }
                  
                    cell.labelName.text = [self.instructionName objectAtIndex:indexPath.row];
                    cell.labelDescription.text = [self.instructionDescription objectAtIndex:indexPath.row];
                  
                    return cell;
                  }
                  
                  - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
                      NSString *cellText = [self.instructionDescription objectAtIndex:indexPath.row];
                      UIFont *cellFont = [UIFont fontWithName:@"Arial" size:15];
                      CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
                      CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
                  
                      return labelSize.height + 25;  
                  }
                  

                  推荐答案

                  如果单元格中的文本发生变化,则需要在 tableView 发生变化时调用 reloadData,否则tableView:heightForRowAtIndexPath: 不会被调用.

                  If the text in the cells is going to change, you will need to a call reloadData on your tableView when it does, otherwise the tableView:heightForRowAtIndexPath: won't be called.

                  tableView 的工作方式是在每次重新加载(以及重新加载变体)时收集所有行的高度.这就是 iOS 知道整个表格高度的方式.它不会在抓取单个单元格时再次调用 tableView:heightForRowAtIndexPath:.通常调用 reloadData 非常快,而且此方法与其变体不同,不会导致任何滚动.

                  The way the tableView works is that it gathers the heights for all rows at every reload (and the reload variants). This is how iOS knows the height of the entire table. It doesn't call tableView:heightForRowAtIndexPath: again when grabbing individual cells. Usually calling reloadData is quite quick and this method, unlike its variants, doesn't cause any scrolling.

                  请参阅 stackoverflow 问答 获取有用的背景信息.

                  See stackoverflow question and answer for useful background.

                  如果您必须做大量工作来计算高度并且您有数百或数千行,就会出现问题.在我有这样的一个用例中,我缓存了行高计算以获得不错的性能.但是,如果这不是您的情况,请对 reloadData 放宽一点.

                  Problems arise if you have to do a lot of work to calculate heights and you have hundreds or thousands of rows. In one use case I have like this, I cache the row height calculations to get decent performance. But if that's not your case, just be a little more liberal with reloadData.

                  这篇关于IOS:具有自定义 uitableviewcell 的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  EAAccessory/EASession teardown during background(EAAccessory/EASession 在后台拆除)
                  Getting an NSArray of a single attribute from an NSArray(从 NSArray 获取单个属性的 NSArray)
                  ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
                  How to get indexPath.row of tableView which is currently being displayed?(如何获取当前正在显示的 tableView 的 indexPath.row?)
                  Xcode iOS organizer submit to app store yields quot;The archive is invalidquot; error(Xcode iOS 管理器提交到应用商店产生“存档无效;错误)
                  MFMessageComposeViewController alloc returns nil(MFMessageComposeViewController alloc 返回 nil)
                  <legend id='EOuhR'><style id='EOuhR'><dir id='EOuhR'><q id='EOuhR'></q></dir></style></legend>
                      <tbody id='EOuhR'></tbody>

                      • <bdo id='EOuhR'></bdo><ul id='EOuhR'></ul>

                          <i id='EOuhR'><tr id='EOuhR'><dt id='EOuhR'><q id='EOuhR'><span id='EOuhR'><b id='EOuhR'><form id='EOuhR'><ins id='EOuhR'></ins><ul id='EOuhR'></ul><sub id='EOuhR'></sub></form><legend id='EOuhR'></legend><bdo id='EOuhR'><pre id='EOuhR'><center id='EOuhR'></center></pre></bdo></b><th id='EOuhR'></th></span></q></dt></tr></i><div id='EOuhR'><tfoot id='EOuhR'></tfoot><dl id='EOuhR'><fieldset id='EOuhR'></fieldset></dl></div>
                          <tfoot id='EOuhR'></tfoot>
                        1. <small id='EOuhR'></small><noframes id='EOuhR'>