表格视图显示错误的单元格大小 [英] Tableview showing the wrong size of cell

查看:29
本文介绍了表格视图显示错误的单元格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格中,我正在使用表格视图的(heightForRowAtIndexPath)委托设置单元格的高度代码是:

in table i am setting the height for cell using(heightForRowAtIndexPath)delegate of table view the code is:

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

但我正在检查委托方法(cellForRowAtIndexPath)中单元格的大小,代码是:

but i am checking the size of cell in delegate method(cellForRowAtIndexPath) and code is:

 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue or create a cell
UITableViewCellStyle style =  UITableViewCellStyleDefault;
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"BaseCell"];
if (!cell) 
    cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] ;

cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", numberOfItems - indexPath.row];
    NSLog(@"%f",cell.frame.size.height);
 return cell;
  }

当我在控制台上打印值(单元格的框架)单元格时,它给了我 44.00.为什么即使我正在设置单元格的高度也会发生这种情况..请解释我以及如何使单元格高度为 100..提前致谢

when i am printing the value(frame of cell)cell on console its giving me 44.00.why this is happening even i am setting height of cell..please explain me and what to do to get the cell oh height 100..thanks in advance

实际上我想制作支持不同视图方向的自定义类型表视图,并且它是通用应用程序,因此最好调用单元格大小来代表每次检查(iphone/ipad,不同方向)....请帮我完成要求

actually i want to make custom type table view which support difrrent orientation of view and it is universal app so it will better to call the cell size in behalf of checking every time (iphone/ipad,diff orintation)....plz help me to accomplish requirement

推荐答案

如果单元格显示正确,并且正确我的意思是高度为 100 像素,正如您在 tableView:heightForRowAtIndexPath:,我很确定这是因为你在错误的地方要求单元格的高度:单元格刚刚使用默认的 init 方法初始化,因此返回的高度是默认值,为 44 像素,作为控制台中的 nslog 提示,在呈现委托时设置从您的方法返回的正确高度,一切都已设置正确.

If the cell is being shown correctly, and by correctly I mean with a height of 100 pixels as you have written in your tableView:heightForRowAtIndexPath:, I'm pretty sure it's because you're asking for the cell's height in the wrong place: the cell has just been init'd with the default init method, the height returned is therefore the default one, of 44 pixels as nslog prompts in your console, on rendering the delegate sets the right height returned from your method and everything is set up correctly.

几个月前我遇到了这个问题,出于某些原因我需要在 tableView:cellForRowAtIndexPath: 方法中知道单元格的高度,我想出了一个解决方法:我已经存储了所有的 rowHeights 值在 NSArray 中,因为它们是动态的,并且根据它们的内容逐行不同.

I had this issue months ago, for some reasons I needed to know cell's height in the tableView:cellForRowAtIndexPath: method, I came out with a workaround for that: I've stored all rowHeights values in an NSArray, since they were dynamic and different row by row according to their content.

我想出了类似的东西

CGFloat height = [[heightsData objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];

CGFloat height = [[heightsData objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];

这篇关于表格视图显示错误的单元格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆