objective-c 如何更改表格视图单元格的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the height of table view cell
提问by monjer
i m trying to read some RSS data. I have the data that is of different sizes. the data is present in tabke view data object. I used the label to add the data and resize the dat. No success. Please help.
我正在尝试阅读一些 RSS 数据。我有不同大小的数据。数据存在于 tabke 视图数据对象中。我使用标签添加数据并调整数据大小。没有成功。请帮忙。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSLog(@"in the tabel view cell");
heightOfCell=[self tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Default"];
if (cell == nil)
{
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
UILabel *label = [[UILabel alloc] init];
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
label.text = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
CGSize labelSize = [[[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
label.lineBreakMode=UILineBreakModeWordWrap;
[label sizeThatFits:labelSize];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
//[label sizeToFit];
[cell addSubview:label];
[label release];
}
}
回答by darshan
you need to use the following method.
您需要使用以下方法。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
you need to change the height as per your requirements
您需要根据您的要求更改高度
It should be noted that the default height is 44.
需要注意的是,默认高度是 44。
回答by ennuikiller
You need to implement the following method:
您需要实现以下方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// some code that compute row's height
}
回答by Faizan Refai
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
// This is just Programatic method you can also do that by xib !
}
You can also change it in Interface Builder. Open the *.xib file, select the table, show the Size Inspector, and change the row height. But, please note that the default height is 44 pixels.
您也可以在 Interface Builder 中更改它。打开 *.xib 文件,选择表格,显示大小检查器,并更改行高。但是,请注意默认高度为 44 像素。
回答by monjer
There are two methods to set the UITableViewCell's height .:
有两种方法可以设置UITableViewCell的高度。:
UITableViewhas a property namedrowHeight, with which you can use to set all theUITableViewCellto the same row height;- If you implement the
UITableViewDelegateprotocol , and includetableView:heightForRowAtIndexPath:method, it's you responsbility to decide the currentUITableViewCell'height. In practice, we always use the delegate method to set the cell's height, only if each cell's height may be dynamic! And the method is preference to theUITableView'srowHeightproperty!
UITableView有一个名为 的属性rowHeight,您可以使用它来将所有的UITableViewCell设置为相同的行高;- 如果您实现了
UITableViewDelegate协议和包含tableView:heightForRowAtIndexPath:方法,则您有责任决定当前的UITableViewCell'height. 在实践中,我们总是使用委托方法来设置单元格的高度,前提是每个单元格的高度可能是动态的!并且方法优先于UITableView的rowHeight属性!
So, I think you may prefer the second method:
所以,我想你可能更喜欢第二种方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return lableSize ;
}
回答by Jose Pose S
I saw a lot of solutions but all was wrong or uncomplet. You no need calculate nothing (no font, no size, nothing)... You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive C:
我看到了很多解决方案,但都是错误的或不完整的。你不需要计算任何东西(没有字体,没有大小,没有)......你可以在 viewDidLoad 和 autolayout 中用 5 行解决所有问题。这对于目标 C:
_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
For swift 2.0:
对于 swift 2.0:
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
Now create your cell with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"
现在用 xib 创建你的单元格或在你的故事板中创建 tableview 有了这个,你不需要再实现或覆盖。(不要忘记数字 os 行 0)和底部标签(约束)降级“内容拥抱优先级——垂直到 250”
You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight
你可以在下一个网址下载代码:https: //github.com/jposes22/exampleTableCellCustomHeight
References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
参考资料:http: //candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
回答by Nick Bedford
You have to know the heights of all cells before the tableView:cellForRowAtIndexPath:delegate call which may require you to store heights inside your view controller or some other list.
您必须在tableView:cellForRowAtIndexPath:委托调用之前知道所有单元格的高度,这可能需要您将高度存储在视图控制器或其他列表中。
The function to customise the heights of cells is tableView:heightForRowAtIndexPath:. The default height for a table view is 44 pixels, for reference.
自定义单元格高度的函数是tableView:heightForRowAtIndexPath:。表视图的默认高度为 44 像素,以供参考。
You can find the documentation here.
您可以在此处找到文档。


