好吧,另一个 UITableViewCell 动态高度问题,但有点扭曲。 不幸的是,我不能只在发布时跳转到 iOS 8,否则问题就解决了。需要 iOS >= 7.1。
我正在尝试实现一个单元格,在单元格顶部有两个图像,在它们下方有一个标题标签,在其下方有一个描述标签。我知道顶部的两个图像是正方形的,因此我希望它们具有相同的大小并保持正方形的纵横比,但在屏幕尺寸变化时调整大小(例如方向改变或不同的设备)。
可能有助于可视化的图像(由于代表不能包含。没关系颜色,可视化帮助): http://i.stack.imgur.com/kOhkl.png
注意最后一个文本之后的空隙,那不应该在那里。
我在以前的应用程序中根据以下内容实现了动态高度: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (成功),现在也使用相同的方法。
我已经使用 Storyboard和编程方式设置了约束,但没有成功。
不过,这是关键。调用时:
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
在:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
高度值远大于阅读时的高度:
cell.contentView.frame
另一个问题是,如果我删除图像的纵横比限制并仅将其更改为明确的高度限制,它就可以正常工作。
对于任何愿意提供帮助的人,我整理了一个非常简单的示例项目来说明问题: https://github.com/alefr/DynamicTableCellHeight
它被设置为使用 Storyboard作为约束,并且有一个额外的分支:ExplicitHeightConstraint,它除了将宽高比约束更改为图像的高度约束外什么都不做。
**所以问题是:** 任何人都可以看到使高度计算混淆的约束有什么问题,或者是否有人有任何替代建议来获得想要的结果? (虽然我想为 View 使用自动布局)。
作品中有很多限制(虽然没有什么特别的)所以我认为查看提供的 Storyboard (github 链接)比在此处明确说明更容易。但是,如果有人认为我也可以这样做。估计的高度计算如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Don't care about memory leaks now:
DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dynamicCell"];
[self populateCell:cell withContent:self.tableData[indexPath.row]];
// Make sure the constraints have been set up for this cell, since it may have just been created from scratch.
// Use the following lines, assuming you are setting up constraints from within the cell's updateConstraints method:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct cell height for different table view widths if the cell's height depends on its width (due to
// multi-line UILabels word wrapping, etc). We don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
// Also note, the final width of the cell may not be the width of the table view in some cases, for example when a
// section index is displayed along the right side of the table view. You must account for the reduced cell width.
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints.
// (Note that you must set the preferredMaxLayoutWidth on multi-line UILabels inside the -[layoutSubviews] method
// of the UITableViewCell subclass, or do it manually at this point before the below 2 lines!)
[cell setNeedsLayout];
[cell layoutIfNeeded];
// Get the actual height required for the cell's contentView
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// So we can read debug values
CGRect contentFrame = cell.contentView.frame;
CGRect firstImageFrame = cell.firstArtwork.frame;
CGRect secondImageFrame = cell.secondArtwork.frame;
CGRect nameFrame = cell.name.frame;
CGRect numArtworksFrame = cell.numArtworks.frame;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}
- (void)populateCell:(DynamicTableViewCell*)cell withContent:(DynamicContent*)content
{
cell.firstArtwork.image = content.firstImage;
cell.secondArtwork.image = content.secondImage;
cell.name.text = content.name;
cell.numArtworks.text = [NSString stringWithFormat:@"%@ artworks", content.numImages];
}
谢谢
最佳答案
我遇到了同样的情况,并解决了它以覆盖给定单元格中的 systemLayoutSizeFittingSize 函数并将 aspectRation 约束替换为高度约束。在下面的示例中,mediaPlayerAspectRationConstraint 在 View 生命周期中被添加到 View 中,mediaPlayerHeightContraint 将用于确定单元格的正确高度(参见下面的代码) .
所以,我用了
CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
而不是 [cell.contentView systemLayoutSizeFittingSize:]。
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);
//Lets replace it with an exact height constraint (workaround).
//To remove the Unable to simultaneously satisfy constraints. exceptions
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
[self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];
[self setNeedsUpdateConstraints];
CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
[self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];
[self setNeedsUpdateConstraints];
return res;
}
关于ios - UIView纵横比混淆systemLayoutSizeFittingSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25664716/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我在Ruby中遇到了一个关于Dir[]和File.join()的简单程序,blobs_dir='/path/to/dir'Dir[File.join(blobs_dir,"**","*")].eachdo|file|FileUtils.rm_rf(file)ifFile.symlink?(file)我有两个困惑:首先,File.join(@blobs_dir,"**","*")中的第二个和第三个参数是什么意思?其次,Dir[]在Ruby中有什么用?我只知道它等价于Dir.glob(),但是,我对Dir.glob()确实不是很清楚。 最佳答案
当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#
当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab
请有人帮助我了解ruby应用程序如何管理应用程序的gemfile和rvmgemsets。如果我当前使用的是Gemset,安装了一堆gem,并且我的gemfile中也有gems,那么Ruby应用程序是使用gemfile中的gem还是应用程序的gemset中的gem? 最佳答案 要理解这一点,您需要退后一步,了解rubygems的一般工作原理。让我们从一个没有rvm或Gemfile的系统开始。当您通过“geminstall”安装gem时,它会进入系统gem位置。每当您编写ruby脚本并需要gem时,它就会从那里获取。现在假设
我需要将目录中的一堆文件上传到S3。由于上传所需的90%以上的时间都花在了等待http请求完成上,所以我想以某种方式同时执行其中的几个。Fibers能帮我解决这个问题吗?它们被描述为解决此类问题的一种方法,但我想不出在http调用阻塞时我可以做任何工作的任何方法。有什么方法可以在没有线程的情况下解决这个问题? 最佳答案 我没有使用1.9中的纤程,但是1.8.6中的常规线程可以解决这个问题。尝试使用队列http://ruby-doc.org/stdlib/libdoc/thread/rdoc/classes/Queue.html查看文
在ruby中...我有一个由外部进程创建的IO对象,我需要从中获取文件名。然而我似乎只能得到文件描述符(3),这对我来说不是很有用。有没有办法从此对象获取文件名甚至获取文件对象?我正在从通知程序中获取IO对象。所以这也可能是获取文件路径的一种方式? 最佳答案 关于howtogetathefilenameinC也有类似的问题,我将在这里以ruby的方式给出这个问题的答案。在Linux中获取文件名假设io是您的IO对象。以下代码为您提供了文件名。File.readlink("/proc/self/fd/#{io.fileno}")例