我用来UITableViewAutomaticDimension计算表格视图单元格的高度。这对我来说可以。但对于某些单元格,返回的高度是 44,而不是动态计算高度。但是,当向上滚动并返回到同一单元格时,高度会被完美地重新计算。所以我想我没有任何错误的约束,因为在我手动滚动并更正所有单元格高度后,之后不会显示任何约束破坏警告。
编辑
下面给出的是图像的屏幕截图(裁剪时混乱,但可以从个人资料图像中看到问题。)。第一个单元高度计算正确。但其余的都是错误的。

objective-c uitableview nslayoutconstraint ios-autolayout uitableviewautomaticdimension
我有一个PFQueryTableViewController我正在尝试学习一些tableView交互.
我目前拥有的:tableView单元格在水龙头上增加其高度,didSelectRowAtIndexPath或者我基本上可以将单元格相关的数据performSegueWithIdentifier与prepareForSegue单个水龙头一起使用.
在下面的代码中,如果我评论"点击代码以增加tableView单元格的高度"代码,我可以检测到双击.其他方面,单击会增加高度,因为代码在didSelect块内.
我需要的:在单击时,细胞高度增加或减少.当细胞高度增加时,如果我双击,它应该将细胞数据分割到下一个UIViewController.
如果没有,单击应增加或减少高度.并且双击应该将细胞数据分割到下一个UIViewController
代码如下:
var selectedCellIndexPath: NSIndexPath?
var SelectedCellHeight = CGFloat() // currently set to 480.0 tableView height
var UnselectedCellHeight = CGFloat() // currently set to 300.0 tableView unselected hight
....
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
return SelectedCellHeight
}
}
return UnselectedCellHeight
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { …Run Code Online (Sandbox Code Playgroud) uitableview ios uitapgesturerecognizer swift uitableviewautomaticdimension
我UITableView跟UITableViewAutomaticDimension一些estimatedRowHeight。对于此表,我正在使用custom UITableViewCell,其中包含一些标签和custom UIView和overridden intrinsicContentSize()。约束设置正确,表格可以确定每行的实际高度。到目前为止,一切都很好。
现在,我开始根据可用宽度修改自定义视图的内部逻辑,以适应其外观,即,当表格单元格大小不够宽时,我的视图可以重新排列子视图以适应新的限制,这会影响最终的高度,所以我有如下代码那:
var internalSize: CGSize = ...
override func intrinsicContentSize() -> CGSize {
return internalSize
}
override func layoutSubviews() {
super.layoutSubviews()
fitIntoWidth(frame.size.width)
}
private func fitIntoWidth(width: CGFloat) {
let height = // calculate based on content and width
internalSize = CGSizeMake(width, height)
invalidateIntrinsicContentSize()
}
Run Code Online (Sandbox Code Playgroud)
现在,当我填充表格视图时,intrinsicContentSize()返回一些所需的值,但它不适用于当前布局,然后控制转到layoutSubviews()重新计算大小并再次调用系统的位置intrinsicContentSize(),现在它返回良好的值。但是,第一次时间表会加载基于错误intrinsicContentSize()值计算的数据和像元高度。如果我再打reloadData()一次,一切都会变好,并且表格中所有后续单元格的布局也都可以。
我的错误在哪里,以及如何修改代码以使单元大小正确工作而无需调用reloadData()两次?
uitableview ios swift uitableviewautomaticdimension intrinsic-content-size
只要我不尝试启用自动调整大小,我的单元格就会显示正常.当我在viewController中添加两行自动调整大小时,配置文件图片,用户名和日期下的所有内容都会消失(当您点击图片时,该单元格应该像Instagram一样).
如果我注释掉tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 450那么单元格显示的确如此,但它显然没有调整大小.
这是我目前的代码:在我的tableViewController中:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 450
Run Code Online (Sandbox Code Playgroud)
在我的cellViewController中
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var usernameBtn: UIButton!
@IBOutlet weak var dateLbl: UILabel!
@IBOutlet weak var mainPic: UIImageView!
@IBOutlet weak var likeBtn: UIButton!
@IBOutlet weak var commentBtn: UIButton!
@IBOutlet weak var moreBtn: UIButton!
@IBOutlet weak var likeLbl: UILabel!
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var uuidLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
let width = UIScreen.main.bounds.width
//allow constraints
profilePic.translatesAutoresizingMaskIntoConstraints = …Run Code Online (Sandbox Code Playgroud) uitableview tableviewcell swift uitableviewautomaticdimension
我有点沮丧。我有一个iOS的10项目UITableView与UITableViewAutomaticDimension单元格。我正确设置了约束。
最初我在里面映射我的细胞
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
一切正常,但后来我读了一篇文章:https : //medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.4rnbc4vyg并将我的数据映射移动到
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
在此之后,我的自动布局单元格停止正常工作。
我的理解是 at height 是在 after cellForRowAtbut before计算的willDisplay,所以当我willDisplay已经为我的单元设置了高度映射时,我需要重新加载它(这不好)。
任何人都可以证明/解释这一点?
更新:
发帖提问后,发现这篇文章:https : //tech.zalando.com/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/
我有一个项目(由其他人编写),其中有一个内容和文本显示在表视图中的提要.每个帖子对应于表格视图中的一个部分,每个部分都有自己的行,对应于内容,文本,按钮等元素.
我需要在表格视图单元格中使用"更多"按钮显示帖子标题的短标签,当点击更多按钮时,标签将扩展到标题适合的任何大小,所有这些都发生在表格视图单元格内.当点击更多按钮时,我将标签的numberOfLines属性更改为零,并且由于单元格具有自动高度,我所需要的只是重新加载特定的标题单元格.(如果我numberOfLines在显示单元格之前在第一个位置设置为0,则单元格会以扩展的大小正确显示.)
我试过了:
[tableView beginUpdates];
tableView endUpdates];
我尝试了各种动画选项:
[tableView reloadRowsAtIndexPaths:@[myPath] withRowAnimation:UITableViewRowAnimation(Bottom,Top,None etc)];
我试过了:
[tableView reloadSections:[NSIndexSet indexSetWithIndex:myPath.section] withRowAnimation:UITableViewRowAnimation(Top,Bottom,None etc)];
但它们都产生相同的结果:整个表视图布局搞砸了:它跳转到另一个单元格,一些视图变为空白,单元格相互重叠,单元格内的视频停止播放,标签不会扩展(但内部刷新)本身,例如,一行的短预览txt从顶部/底部等动画,但不会展开).
什么可能导致整个表视图的混乱,以及如何正确地重新加载一个单元格和扩展动画,而不会弄乱整个布局?我已经看到了很多关于此的问题和答案,但他们都推荐了我已经尝试过并在上面解释过的选项.
我的应用程序面向iOS 8.0+
更新:这是相关代码(有关内部工作的一些部分与布局无关,已删除):
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier: MyCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.indexPathToReloadOnAnimation = indexPath;
cell.shouldShortenCaption = YES;
id post = self.posts[indexPath.section] ;
[cell setPost:post];
return cell;
Run Code Online (Sandbox Code Playgroud)
内部setPost::
if(self.shouldShortenCaption){
captionLabel.numberOfLines = 2;
}else{
captionLabel.numberOfLines = 0;
}
NSString *text = [some calculated (deterministic) text from post object];
Run Code Online (Sandbox Code Playgroud)
按钮动作很简单:
self.shouldShortenCaption = NO; …Run Code Online (Sandbox Code Playgroud)
我在 UITableViewCell 中有一个 UIView ,当用户点击“更少/更多”按钮时需要更改其高度
我的代码是
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud) objective-c uitableview ios ios-autolayout uitableviewautomaticdimension
我正在使用iOS9 XCode7我需要根据labelText高度动态更改单元格的高度
我用过:
self.tableView.rowHeight=UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)
但它不适用于定制电池.
sizetoFit在iOS9中删除.
请提出建议.谢谢
在我的UITableView使用方法滚动到最后一个单元格:
WLNetworkClient.sharedClient().createCommentWIthText(commentTextView.text, forItem: item) { error in
defer {
UIAlertController.showAlertFromError(error)
}
if error == nil {
self.fetchedResultsController.fetchRequest.fetchLimit += 1
try! self.fetchedResultsController.performFetch()
self.tableView.reloadData()
self.scrollTableViewToBottom()
}
}
private func scrollTableViewToBottom() {
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: fetchedResultsController.fetchedObjects!.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
但是这没有按预期工作,因为ALWAYS表从顶部滚动,即使我在第二个单元格上也是如此.如何解决这个问题?
我有一个可通过 nib/xib 文件重用的 UIView。我想加载它并填充一个 UITableViewCell,它将在一个自动调整大小的 UITableView 中使用。全部带有自动布局。
大多数效果很好,但似乎加载的 UIView 使用周围添加的约束来缩小 UITableViewCell 的 contentView。这对高度有好处,但我不想要这个宽度。
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cellId = "RowView0002"
var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
let subView = RowView(frame: cell!.frame)
cell!.contentView.attachViewWithConstraints(subView)
let _ = subView.viewLoadedFromNibAttached(name: cellId)
}
return cell!
}
override public func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
}
extension …Run Code Online (Sandbox Code Playgroud) uitableviewautomaticdimension ×10
uitableview ×9
ios ×7
objective-c ×4
swift ×4
autolayout ×2
ios9 ×1