我正在使用子类UITableViewCell,我需要在使用自动布局时将UILabel的文本对齐到左上角.我意识到读取sizeToFit确实不应该与自动布局一起使用,我想避免它,并以某种方式使用约束.基本上,每次重复使用单元时都会重置标签的文本,因此在重用时,大小调整需要是动态的.
这是子类化单元格内的Lazy初始化程序标签:
- (UILabel *)commentsLabel {
if (_commentsLabel == nil) {
_commentsLabel = [[UILabel alloc] init];
[_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
_commentsLabel.numberOfLines = 0;
_commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
_commentsLabel.preferredMaxLayoutWidth = 100;
}
return _commentsLabel;
}
Run Code Online (Sandbox Code Playgroud)
标签上设置了自动布局约束(commentsLabel是添加到单元子类上的self.customView的子视图):
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
options:0
metrics:@{
@"locationToTopPadding":@(locationToTopPadding),
@"locationHeight":@(locationHeight),
@"locationToCommentsPadding":@(locationToCommentsPadding),
@"commentsToBottomPadding":@(commentsToBottomPadding)
}
views:viewsDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
options:0
metrics:@{@"commentsWidth":@(commentsWidth)}
views:viewsDictionary]];
Run Code Online (Sandbox Code Playgroud)
设置只是:
self.commentsLabel.preferredMaxLayoutWidth = 100;
Run Code Online (Sandbox Code Playgroud)
虽然在大多数答案中提到过,但似乎没有用.
目前有这个实施但没有看到任何结果. UILabel sizeToFit不适用于autolayout ios6
我试过的另一个答案. UILabel sizeToFit仅适用于关闭AutoLayout
我觉得我只缺少1个约束,除了上面的约束之外还可以添加,但我尝试添加的程序性约束不起作用或抛出异常.我完全在代码中工作没有xib.
ETA:尝试在现有垂直约束线内设置高度约束:
像这里建议:动态改变UILabel宽度不适用于autolayout
在上面的垂直约束线中:
-[commentsLabel(>=30@900)]-
Run Code Online (Sandbox Code Playgroud)
我已经弄乱了高度值和优先级值,没有任何改变.
ETA:取得一些进展,我认为它与标签的底部填充有关,试过这个并且一些标签正确对齐有些不是:
->=commentsToBottomPadding-
Run Code Online (Sandbox Code Playgroud) 我尝试在自定义uitableviewcell中使用autolayout并尝试根据此SO主题实现动态高度
在UITableView中使用自动布局来获取动态单元格布局和可变行高
但我的单元格是由xib文件创建的.所以方法有些不同这是我在heightForRowAtIndexPath中的代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
myDataItem *item= [[data items] objectAtIndex:[indexPath row]];
CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
if (!cell) {
cell = [CustomCell alloc] init];
[self.offscreenCells setObject:cell forKey:@"CustomCell"];
}
[cell.commentView setText:item.comment];
[cell.displayNameView setText:item.displayNameOfItemOwner];
[cell.timeAgoView setText:item.timeAgo];
[cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height+1; }
Run Code Online (Sandbox Code Playgroud)
我从[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]获得的高度总是"无""当我尝试调试时,我发现我的customcell的所有子视图都是"nil",但是单元格本身不是"nil".可能是因为我的customCell是由nib创建的,当我[[CustomCell alloc] init]时,单元格没有完全创建.
但我确实尝试将方法更改为cell = [_ tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
结果仍然相同.每个子视图都是零.这使得高度返回为零并且使得我的tableView显示不正确.有什么方法可以解决这个问题吗?
我尝试在AutoLayout上制作这些.实际上,在之前的版本中,我没有使用AutoLayout并手动计算单元格高度.它确实工作得很好.无论如何,我只想尝试AutoLayout.
请帮忙.谢谢
使用autolayout时,我在UITableViewCell内部遇到UIImageView问题.具体来说,我想将图像与边缘(20pt)分开,具有严格的尺寸(50pt),因此我已经定义了这个:imageview到top =导致edgeview = 20 height = width = 50

我发现imageview完全忽略了它的限制(抱歉可怕的笑脸,这是谷歌图片给我的).当然,我想要的布局会有更多内容,但无论内容是否存在,效果都是一样的.

项目的设置如下:创建单个视图应用程序添加表格视图,连接数据源并编写样板文件以使至少一个单元格创建带有nib的tableviewcell,将tableviewcell约束的imageview添加到top/leading( 10个作品,高度为20)在imageview中设置图像,以便您可以在运行时看到它
我觉得这应该足够了,但似乎不起作用,我错过了一些愚蠢的东西吗?为了进行比较,如果没有tableview,这个相同的设置没有问题,imageview的行为就像预期的那样
我试图在UITableViewCell下面做一个类似的,它将根据其内容视图调整其高度.中心的图像应与细胞框架一样宽,并按比例调整其高度.

如您所见,单元格中的图像不会像我想要的那样显示.我试图将高度设置UIImageView为320.0英寸override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell,但根本没有帮助.
问题:如何让细胞尊重图像的高度?
供参考:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloatUIImageView或任何东西,我只使用填充来安排视图.编辑:
我进一步向图片添加了一个高度约束:
class MyTableViewPostCell: UITableViewCell {
@IBOutlet weak var contentLabel: UILabel!
@IBOutlet weak var pictureImageView: UIImageView!
@IBOutlet weak var pictureImageViewHeightConstraint: NSLayoutConstraint!
}
class MyTableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> …Run Code Online (Sandbox Code Playgroud) 当我搜索如何实现iOS中自动调整大小电池我碰到过很多例子(这里 这里,并在这里与这个神秘的代码)- (CGFloat)heightForImageCellAtIndexPath:(NSIndexPath *)indexPath
static CommentedItemCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
});
Run Code Online (Sandbox Code Playgroud)
但我找不到这个dispatch_once背后的原因.我认为它的目的是节省一些记忆,但为什么这种风格.为什么不定义属性和延迟加载它.
@property (nonatomic, strong) UITableViewCell sizingCell;
同
- (UITableViewCell)getSizingCell
{
if (_sizingCell) return _sizingCell;
_sizingCell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
return _sizingCell;
}
Run Code Online (Sandbox Code Playgroud)
想知道它的编码风格,或者这个dispatch_once实现背后有一些好处.
在界面构建器中,我正在尝试创建一个原型单元格,其中的图像覆盖整个单元格,但它没有按预期运行.
正如您在我的界面构建器的以下屏幕截图中所看到的,我有一个覆盖整个单元格的图像视图,并且被约束到单元格的每个边缘:
事实上这就是我期望它在模拟器上的样子,但我得到了这个:
在你可以看到的地方,它并没有一直固定到两侧,而且可能很难看到,但是图像实际上延伸到了单元格的底部(如果你看起来很努力,你可以看到分隔符穿过图像的底部.
这真是个错误,我真的不知道发生了什么.
我遵循/sf/answers/1312285131/中提到的概念,但稍微以不同的方式使用自动布局实现动态单元格.我只有一个原型自定义单元格.但我必须根据数据阵列向单元格添加多个UILabel,并且必须保持动态单元格高度.
//我的UITableViewCell
@interface CustomCell ()
@property (nonatomic, assign) BOOL didAddLabel;
@end
@implementation CustomCell
- (void)awakeFromNib {
[super awakeFromNib];
}
-(void)addLabel:(NSArray*)someAry{
if(!didAddLabel){
for (int i=0; i<someAry.count; i++) {
// Initialize UILabel Programtically ...
// adding label
[self.aViewOfContaintView addSubview:aLabel];
}
self.heightLayoutConstraintOfaViewOfContaintView.constant = value;
[self.aViewOfContaintView layoutIfNeeded];
self.didAddLabel = YES;
}
}
@end
Run Code Online (Sandbox Code Playgroud)
//我的UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
//.. .. ..
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;
//.. .. ..
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = …Run Code Online (Sandbox Code Playgroud) 在我的 iOS 应用程序中,我想显示几个包含一些 HTML 的项目。为此,我构建了一个包含UITableView的NewsListViewController。对于那个UITableView (outlet: newsListTableView ),我创建了一个名为NewsTableViewCell的自定义UITableViewCell,它将通过委托加载到其中。该NewsTableViewCell持有WKWebKit控制,将显示我的HTML。这是我的代码:
NewsListViewController.swift
import UIKit
class NewsListViewController: UIViewController {
@IBOutlet weak var newsListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.newsListTableView.delegate = self
self.newsListTableView.dataSource = self
self.newsListTableView.register(UINib(nibName: "NewsTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
}
}
extension NewsListViewController: UITableViewDelegate {
}
extension NewsListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: …Run Code Online (Sandbox Code Playgroud) 我没有添加任何原型单元格,但它应该根据最新的iOS 8 tableView工作.这是我的代码
class ViewController: UIViewController,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tabledata = ["hn, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjks" , "hi i am ishan, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjkssndjkasndjksandkjasndkjasndkjasndkjasndjka ", "a" ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tabledata.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text …Run Code Online (Sandbox Code Playgroud) ios ×10
uitableview ×7
autolayout ×6
objective-c ×5
swift ×5
iphone ×2
uiimageview ×2
autosize ×1
xcode ×1
xib ×1