我正在使用AutoLayout实现我的tableview单元格来显示图像和2个标签,使用动态类型来显示自适应字体大小.
我实现了estimatedHeightForRowAtIndexPath,它非常有意义且易于使用.
我没有为单元格使用界面构建器.相反,我使用砌体,这应该没有任何区别.
我目前正在努力计算实际的细胞高度.在更新自动布局代码时,手动计算是一件痛苦的事情并且难以维护.
我发现这个StackOverFlow答案:在UITableView中使用自动布局来获得动态单元格布局和可变行高度 此解决方案还应该考虑不同的字体大小,但对我来说不起作用.
当我有这样的AutoLayout系统时:UILabel顶部带有填充的contentView,另一个UILabel到带有填充的contentView底部,它应该自动计算单元格高度.
但相反,它会导致以下AutoLayout冲突:
UIView property translatesAutoresizingMaskIntoConstraints)
(
"<MASLayoutConstraint:0xc506be0 UIImageView:0xc5070a0.height == 150>",
"<MASLayoutConstraint:0xc506d90 UIImageView:0xc5070a0.top == UITableViewCellContentView:0xc5076e0.top + 10>",
"<MASLayoutConstraint:0xc506990 UILabel:0xc507450.top == UIImageView:0xc5070a0.bottom + 10>",
"<MASLayoutConstraint:0xc506630 UILabel:0xc507260.top == UILabel:0xc507450.bottom>",
"<MASLayoutConstraint:0xc506530 UILabel:0xc507260.bottom == UITableViewCellContentView:0xc5076e0.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0xc3d05a0 UITableViewCellContentView:0xc5076e0.height == 44>"
)
Run Code Online (Sandbox Code Playgroud)
我使用以下AutoLayout代码:
[self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom);
make.right.equalTo(self.contentView.mas_right).with.offset(-GCBaconCellRowPadding);
make.left.equalTo(self.contentView.mas_left).with.offset(GCBaconCellRowPadding);
make.bottom.equalTo(self.contentView.mas_bottom);
}];
Run Code Online (Sandbox Code Playgroud)
最后一行应表明单元格的高度自身延伸.
查看AutoLayout冲突的输出,看起来它想要自动将高度设置为44.0,这是默认值.
编辑:将contentView的translatesAutoresizingMaskIntoConstraints设置为NO
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)
创建单元格时会修复碰撞,但会导致行高为零.
我正在使用 TableView 并有一个自定义的 TableViewCell,我已经向其中添加了一个子视图。
问题是我有时需要更改子视图的高度,因此,必须更新表的 contentView 以及行的高度。
自定义 TableViewCell 的子视图以黄色背景表示。
这些图像显示了我的模拟器中当前发生的情况。
负载

在导致子视图高度增加的事件之后

处理这样的事情的最佳方法是什么?
我应该使用约束吗?如果是这样,我应该使用什么样的约束?每次子视图的大小发生变化时,我是否也必须重新加载 tableview?
这是我目前用于自定义 TableViewCell 的代码:
import UIKit
class CustomCell: UITableViewCell {
var newView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.newView = UIView(frame: self.frame)
self.newView.backgroundColor = .yellowColor()
self.addSubview(newView)
}
required init(coder aDecoder: NSCoder) { …Run Code Online (Sandbox Code Playgroud) 我已经在我的应用程序中创建了自定义单元格.我想要获取每个单元格HeightForRowAtIndexPath.请告诉我如何在此方法中获取自定义单元格.我已尝试此代码但这会导致无限循环并最终导致应用程序崩溃.
HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
编辑:
我试过这个,但它给我的单元格高度为零.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
请告诉我哪个是最好的方法?
我在 tableviewcell 内的 UILabels 中截断文本有问题。到目前为止,我已阅读并测试了以下链接中的解决方案(以及许多其他类似链接):
但如果我使用“标准”顶部、前导、尾部和底部自动布局约束设置单元格,我似乎无法让它在第一次加载时工作。但是,如果我取消选中超级视图项的“相对边距”并使用例如等于:默认值,我可以
如果我旋转设备或再次向下和向上滚动,它适用于这两种情况。
标签与自定义单元上的出口相连,并在原型单元中垂直放置;它们每个都有三个超级视图约束,标签之间有一个“标准”的垂直距离。拥抱优先级在最上面的标签上设置得更高,以消除 IB 中的警告,即使这对结果无关紧要。
相关代码:
TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
self.tableView.estimatedRowHeight = self.tableView.rowHeight;
self.tableView.rowHeight = UITableViewAutomaticDimension; }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCellIB *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];
cell.titleLabel.text = @"Veeeeeeeery looooooooooooooooong Tiiiiitle";
cell.messageLabel.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut …Run Code Online (Sandbox Code Playgroud) 我试图在基本单元格中创建一个带有大文本的表.表位于UIVIewController中,它实现了Delegate和DataSource.
这是我的ViewController:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let answers = ["Swift is a multi-paradigm, compiled programming language created by Apple Inc. for iOS, OS X, and watchOS development. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code than Objective-C, and also more concise. "]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func …Run Code Online (Sandbox Code Playgroud) 我需要使用UITableView 更改cellForRowAtIndexPath中的行高.
heightForRowAtIndexPath在cellForRowAtIndexPath之前执行,但在cellForRowAtIndexPath中我将内容设置为行,取决于必须重新计算的高度.
我有 2 个Label单元格,其中包含来自后端的动态内容,并尝试在TableView. 如何正确地做到这一点?这个设置自定义 UITableViewCells 高度对我没有帮助
在代码中是否有一种方法可以向UITableViewCell添加一些隐藏信息?
使用案例:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath调用并且每个单元格都有一个活动.每个都有自己独特的"activityId".我对IBOutlet的代码是:
- (IBAction)buttonUp:(UIButton *)sender {
UIButton *btn = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *) [[btn superview] superview];
UIView *cellView = [cell.contentView viewWithTag: ..myUniqueButtonTag.. ];
NSLog(@"activityId is: %@", ...);
}
Run Code Online (Sandbox Code Playgroud) uitableview ×8
ios ×6
objective-c ×4
swift ×4
autolayout ×1
cell ×1
cocoa-touch ×1
height ×1
ipad ×1
iphone ×1
row ×1
tableview ×1
xcode ×1
xcode6 ×1