2015-10-20 152 views
1

我在我的应用程序中创建了自定义单元格。我想要获取HeightForRowAtIndexPath中的每个单元格。请告诉我如何在此方法中获得自定义单元格。我试过这段代码,但这会导致无限循环&终于会崩溃应用程序。如何在heightForRowAtIndexPath中获取单元格?

HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath]; 

编辑:

我曾经试过,但它给了我细胞高度为零。

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

    static NSString *cellIdentifier = @"HomeCell"; 
    HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    float tv_view_height=cell.tv_post.frame.size.height; 
    float like_count_height=cell.label_like_count.frame.size.height; 
    float first_comment_height=cell.first_comment.frame.size.height; 
    float second_comment_height=cell.second_cmment.frame.size.height; 
    float third_comment_height=cell.third_comment.frame.size.height; 

    Post *user_post=[arr_post objectAtIndex:indexPath.row]; 
    float comment_count=[user_post.comment_count intValue]; 
    if(comment_count<=0) 
    { 
     first_comment_height=0; 
     second_comment_height=0; 
     third_comment_height=0; 


    } 
    else if(comment_count==1) 
    { 
     second_comment_height=0; 
     third_comment_height=0; 

    } 
    else if(comment_count==2) 
    { 

     third_comment_height=0; 
    } 
    float like_count=[user_post.like_count intValue]; 
    if(like_count<=0) 
    { 

     like_count_height=0; 
    } 
    float total_height=tv_view_height+like_count_height+first_comment_height+second_comment_height+third_comment_height; 
    NSLog(@"total heigh is %f'",total_height); 
    return total_height; 
} 

请告诉哪个是最好的方法?

+0

告诉我你要做什么? – jigs

+0

在'heightForRowAtIndexPath'里面使用'dequeueReusableCellWithIdentifier'不是一个好主意。 – 0yeoj

+0

你建议一些更好的plaese。 – Techiee

回答

0

你可以从头开始创建一个新的电池,只需通过HomeCell *sexyCell = [[HomeCell alloc]init];

或出队一个像你cellForRowtableView.dequeueWithReuseIdentifier:)一样。

虽然我建议创建一个从无到有后处置它(设置为无),因为如果你出列,它在那里,他们将在队列中去,造成重大内存泄漏和许多细胞相同的结束indexPath。

你可以做的是:

  • 创建的alloc初始化细胞
  • 在其视实际数据
  • 使用.layoutsubviews填充它
  • 计算它的大小和应用它到你的真实细胞

你应该怎么做:

使用自动布局并添加必需的所有约束,您的所有标签都将动态调整大小。大约需要3到4个小时才能获得Auto layout的基础知识,并且需要大约一个月的时间才能真正掌握它。

我强烈强烈建议您不要使用对象框架调整大小,如果正确使用约束,大多数标签和视图都将调整大小,而不必编写任何代码。

一旦你这样做了,因为你有不同高度的单元格,所以使用了tableview的DynamicHeight属性和它自带的轻微调整。你可以找到

A great tutorial here

The same updated tutorial for swift(更多的是最新的,但你需要翻译)

This amazing StackOverflow answer which you MUST read

+0

我已经试过这个,但是这给了我错误的结果。像第四个单元格的图像显示在第一个单元格上。 – Techiee

+0

您想要在heightForRow中创建单元格的唯一原因是对高度进行一些计算,例如动画。如果由于cellForRow:你得到错误的数据。除非您使用高度为FowRow的出列,否则会导致问题。 –

+0

其实只是发布你的两种方法(cellforrow和heightforrow),所以我可以看到完整的代码 –

0

我会建议你采取高度形成对你的viewController配置集合。

像这样:

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

    CGFloat height; 
    CellConfiguration * selectedCellConfiguration =[_cellConfigurations objectAtIndex:indexPath.row]; 

    switch (selectedCellConfiguration.type) { 
     case TheTypeYouNeed: 
      return TheValueYouNeed 
     default: 
      height = 44.0f; 
      break; 
    } 

    return height; 
} 
-2

NSIndexPath * indexPath = [NSIndexPath indexPathForRow:indexpath。row inSection:0];

Custom Cell * cell = [tableview cellForRowAtIndexPath:indexPath];

通过这种方式,您将获得每个单元在你的方法

+0

这将使无限循环 – Techiee

+0

通过你将能够获得每个单元格,那么你必须返回一个单元格高度。而且你的应用在返回后不会崩溃。 –

+0

它正在崩溃,我第一次尝试它。 – Techiee

7

如何让细胞在heightForRowAtIndexPath?

这是不可能的,因为当调用-heightForRowAtIndexPath时,尚未创建单元格。您需要了解UITableView如何工作的:

  1. 的UITableView询问它的数据源,将有多少部具有

    -numberOfSectionsInTableView

    在这一点上有没有产生细胞。

  2. 的UITableView询问它的数据源的每个部分将有多少行有

    -numberOfRowsInSection

    在这一点上有没有产生细胞。

  3. 的UITableView让每个可见行的它的代表高度,知道在哪里细胞将位于

    -heightForRowAtIndexPath

    在这一点上有没有产生细胞。

  4. 的UITableView要求它的DataSource给它一个细胞在给定的索引路径

    -cellForRowAtIndexPath

    显示在这一点上创建的细胞。

可以从数据模型计算出的每个单元的高度。您不需要单元格 - 您已经知道将包含注释的框架宽度,您知道它是内容,您知道它是字体,您知道换行模式等。因此,您可以计算高度。例如:

CGFloat commentsHeight = 0; 
Post *user_post = [arr_post objectAtIndex:indexPath.row]; 

for (NSString *comment in user_post.comments) 
{ 
    CGRect commentrect = [comment boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 18, FLT_MAX) 
              options:(NSStringDrawingUsesLineFragmentOrigin) 
             attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} 
              context:nil]; 
    commentsHeight += commentrect.size.height; 
} 

而且您可以从其数据模型中计算单元其他组件的高度。

但现在,在2015年,这不是最好的方法。你真的会更好阅读教程,其中显示@Zil,与Autolayout

1

你应该声明一个数组存储的TableView细胞的cellForRowAtIndexPath,您可以在heightForRowAtIndexPath使用存储单元。让我们尝试使用这个。

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

    HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) { 
      cell = [[[HomeCell alloc] init] autorelease]; 
    } 

    // Store table view cells in an array 
    if (![tableViewCells containsObject:cell]) { 
      [tableViewCells addObject:cell]; 
    } 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([tableViewCellsArray objectAtIndex:indexPath.row]) { 
      HomeCell *cell = (HomeCell *)[tableViewCells objectAtIndex:indexPath.row]; 
      // Process your Code 
    } 

    return yourCalculatedCellHeight; 
} 
相关问题