如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?
自上周以来我一直试图解决这个问题,而没有更进一步.好的,所以我需要在Swift中以编程方式应用一些约束 来使用这段代码:UIView
var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100));
new_view.backgroundColor = UIColor.redColor();
view.addSubview(new_view);
var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
self.view.addConstraint(constX);
var constY:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);
self.view.addConstraint(constY);
var constW:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0);
self.view.addConstraint(constW);
var constH:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: …Run Code Online (Sandbox Code Playgroud) 在我运行包含tableview单元格的应用程序后更新到Xcode 6.1 beta 2后,调试助手说:
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
Run Code Online (Sandbox Code Playgroud)
之前,当我在这个项目上使用Xcode 5时,我会遇到一些错误,但是自从我升级后它们已经消失了.我现在没有其他错误或警告.我已经尝试调整所有tableview单元格的大小,并尝试使用标准高度,但我仍然得到相同的警告:
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
Run Code Online (Sandbox Code Playgroud)
我也阅读了所有类似的主题,但他们的解决方案都没有帮助.当我使用模拟器测试应用程序时,应用程序运行正常,除了应该在tableView单元格中的图片不存在.
有没有人知道如何调试这个?
仅警告一次:检测到约束模糊地建议tableview单元格的内容视图的高度为零的情况.我们正在考虑无意中崩溃并使用标准高度.
行具有固定的高度,如下所示
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 34.0;
}
Run Code Online (Sandbox Code Playgroud)
所有人constraints似乎都很开心......
我有一个带有几个IBOutlets的自定义单元类.我已将该课程添加到故事板中.我连接了所有的插座.我的cellForRowAtIndexPath函数如下所示:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SwipeableCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义单元类:
class SwipeableCell: UITableViewCell {
@IBOutlet var option1: UIButton
@IBOutlet var option2: UIButton
@IBOutlet var topLayerView : UIView
@IBOutlet var mainTextLabel : UILabel
@IBOutlet var categoryIcon : UIImageView
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我的所有单元格都是空的.我已经注销self.venueService.mainCategoriesArray(),它包含所有正确的字符串.我也试过把一个实际的字符串等于标签,这会产生相同的结果.
我错过了什么?任何帮助表示赞赏.
我正在使用以编程方式创建的自定义单元格的表格视图,不使用IB.我在谷歌周围环顾四周并询问过NSChat,但我仍然无法让它发挥作用.它只显示默认的表视图.提前致谢!
EventCell.swift
import UIKit
class EventCell: UITableViewCell {
var eventName: UILabel = UILabel()
var eventCity: UILabel = UILabel()
var eventTime: UILabel = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(eventName)
self.contentView.addSubview(eventCity)
self.contentView.addSubview(eventTime)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
eventName = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width - 40, 25))
eventCity = UILabel(frame: CGRectMake(0, 0, 0, 0))
eventTime = UILabel(frame: CGRectMake(0, 0, 0, 0))
}
}
Run Code Online (Sandbox Code Playgroud)
ViewController.swift
class ViewController: UITableViewController, UITableViewDelegate …Run Code Online (Sandbox Code Playgroud) 我有一个使用自动布局约束的tableview,每个东西都在iOS 7中工作,但是当我在iOS 8中测试时,请给我以下警告
仅警告一次:检测到约束模糊地建议tableview单元格的内容视图的高度为零的情况.我们正在考虑无意中崩溃并使用标准高度.
在我对此问题进行深入调查后,我发现应该仅在iOS8的viewdidload中添加以下行
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 87;
Run Code Online (Sandbox Code Playgroud)
在那之后我仍然得到这个警告,并且单元格的高度不正确,这不是从故事板获取高度
有关UITableViewCell的更多信息,请在下面找到我们对内容视图单元格的约束
-(void) updateConstraints {
[super updateConstraints];
if(!didSetupConstraints ) {
didSetupConstraints = YES;
[self.contentView removeConstraints:self.contentView.constraints];
// Interval Title
//Leading
constraint = [NSLayoutConstraint constraintWithItem:self.intervalTitle attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier: 1.0 constant:0.0];
[self.contentView addConstraint:constraint];
//Top
constraint = [NSLayoutConstraint constraintWithItem:self.intervalTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.marketLocationTitle attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
[self.contentView addConstraint:constraint];
}
Run Code Online (Sandbox Code Playgroud) 我在SO上探讨了关于这个问题的现有问答,但没有找到答案.
我知道这是由tableview在运行时不知道自定义单元格的高度引起的,但不知道如何克服这个问题.这是iOS 8 + Xcode 6.我为自定义单元格的内在大小做了所有自动布局所需的方法...
带有标准单元格的标准tableview,在代码中只创建一个(row = 2)作为自定义单元格;
customCell:
-(CGSize)intrinsicContentSize
Run Code Online (Sandbox Code Playgroud)
{//硬编码用于测试目的
return CGSizeMake(500.0, 450.0);
Run Code Online (Sandbox Code Playgroud)
}
在iOS模拟器上运行时,发现customCell显示在其行下方的其他单元格下方,高度标准,与设置其他标准单元格高度相同,而不是其高度设置比其他单元格大得多.
在表视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
pageViewCellTableViewCell* customCell;
if (indexPath.row != 2)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"regularCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Table Row %lu", indexPath.row];
return cell;
}
else
{
customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell"] ;
if (customCell == nil) {
customCell = [[customCell alloc] init];
}
customCell.parentViewController = self;
[customCell setNeedsUpdateConstraints];
[customCell setNeedsLayout];
[customCell setNeedsDisplay];
return customCell;
}
return …Run Code Online (Sandbox Code Playgroud) uitableview ×7
ios ×5
objective-c ×4
swift ×4
autolayout ×3
ios8 ×3
cocoa-touch ×1
nsautolayout ×1
row-height ×1
storyboard ×1
uiview ×1
xcode ×1