我想在完成表演后滚动到UITableView的底部 [self.tableView reloadData]
我原本有
[self.tableView reloadData]
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Run Code Online (Sandbox Code Playgroud)
但后来我读了reloadData是异步的,所以滚动不因为发生self.tableView,[self.tableView numberOfSections]并且[self.tableView numberOfRowsinSection都是0.
谢谢!
有什么奇怪的是我正在使用:
[self.tableView reloadData];
NSLog(@"Number of Sections %d", [self.tableView numberOfSections]);
NSLog(@"Number of Rows %d", [self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1);
Run Code Online (Sandbox Code Playgroud)
在控制台中它返回Sections = 1,Row = -1;
当我完成相同的NSLogs时,cellForRowAtIndexPath我得到Sections = 1和Row = 8; (8是对的)
我有UICollectionView一个FLowLayout.它会像我期望的那样工作,但是偶尔会有一个单元格无法正确包装.例如,如果实际上在第二行中尾随,则应该在第三行的第一个"列"中打开的单元格,并且它应该是一个空的空间(参见下图).所有你能看到的这个胭脂细胞是左手边(其余的被切掉),它应该是空的.
这不会持续发生; 它并不总是同一行.一旦它发生了,我可以向上滚动然后再回来,单元格将自行修复.或者,当我按下单元格(通过按钮将我带到下一个视图)然后弹回时,我将看到单元格位于错误的位置,然后它将跳转到正确的位置.
滚动速度似乎可以更容易地重现问题.当我慢慢滚动时,我仍然可以偶尔看到单元格处于错误的位置,但是它会立即跳到正确的位置.
当我添加插件插件时,问题就开始了.以前,我的细胞几乎与集合边界齐平(很少或没有插入),我没有注意到这个问题.但这意味着集合视图的右侧和左侧是空的.即,无法滚动.此外,滚动条没有向右冲洗.
我可以在模拟器和iPad 3上解决问题.
我猜这个问题正在发生,因为左右部分插入...但如果值是错误的,那么我希望行为是一致的.我想知道这可能是Apple的错误吗?或许这可能是由于插入物的堆积或类似的东西.

跟进:我一直在使用尼克的这个答案超过2年,现在没有问题(如果人们想知道答案中是否有任何漏洞 - 我还没有找到任何漏洞).做得好尼克.
对于我们使用的UICollectionView动态高度单元,
if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
Run Code Online (Sandbox Code Playgroud)
对于高度和宽度的适当约束,它适用于iOS 11.*版本,但它破坏并且不会使iOS 12.0的单元格动态
uicollectionview uicollectionviewlayout swift uicollectionviewflowlayout ios12
我有一个UICollectionViewCell子类,该子类使用原型单元和约束进行设置(每个视图都垂直和水平连接)。
我有两个大小不一的标签,它们可以是一行或两行。为此,我在每个标签上设置了两个高度限制,其中一个高度大于或等于(取决于标签为16或20)而小于或等于(取决于标签为32或40)。
标签上的行数设置为0。(尽管我尝试了各种设置)。
由于我使用了自动布局和约束来设置视图,因此我在单元格的awakeFromNib()中的内容视图上指定了宽度
override func awakeFromNib() {
NSLayoutConstraint.activate([contentView.widthAnchor.constraint(equalToConstant: 341)])
super.awakeFromNib()
configureBorder()
}
Run Code Online (Sandbox Code Playgroud)
在collectionView的布局上,我已经为EstimateItemSize和itemSize指定了automaticDimension常量。
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
flowLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
}
Run Code Online (Sandbox Code Playgroud)
关于使用自动布局的想法,我尚未在集合视图上实现委托。
我遇到了非常不一致的布局,在某些情况下该单元格会按照我的意愿出现,在另一些情况下,标签被截断了,在另一些情况下,标签却非常狭窄,并在三行上显示了文本。
我尝试了很多事情,包括
.sizeToFit()
在设置了标签的text属性之后调用
每个标签,以及
.setNeedsDisplay()
在单元格从其返回之前调用
cellForItemAt
我想知道使用情节提要约束执行此操作的正确方法是什么。在SO上,我已经针对类似问题解答了一些流行的答案,尽管并没有取得任何真正的成功。
我不知道为什么设计能够适应其内容的单元格如此复杂。它不应该需要那么多代码,我仍然不明白为什么UIKit不能正确处理这个问题。
无论如何,这是我的问题(我已经编辑了整篇文章):
我有一个UICollectionViewCell包含UITableView.
这是我的sizeForItem方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
var cellWidth: CGFloat = collectionView.bounds.size.width
var cellHeight: CGFloat = 0
let cellConfigurator = items[indexPath.item].cellConfigurator
if type(of: cellConfigurator).reuseId == "MoonCollectionViewCell" {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: type(of: cellConfigurator).reuseId, for: indexPath) as? MoonCollectionViewCell {
cell.contentView.layoutIfNeeded()
let size = cell.selfSizedTableView.intrinsicContentSize
cellHeight = size.height
}
}
return CGSize.init(width: cellWidth, height: cellHeight)
}
Run Code Online (Sandbox Code Playgroud)
sizeForItem之前调用过cellForItem …
uitableview ios uicollectionviewcell swift intrinsic-content-size
我想从左到右布局单元格。所以我使用UICollectionViewFlowLayout:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// use this for let cell width fit for label width
layout.estimatedItemSize = CGSizeMake(80, 30);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
Run Code Online (Sandbox Code Playgroud)
单元格的宽度适合标签的宽度:
// cell code
@implementation CQGoodsSearchCell
- (void)setupUI {
self.contentView.layer.cornerRadius = 15;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.00];
self.nameLabel = [[UILabel alloc] init];
[self.contentView addSubview:self.nameLabel];
self.nameLabel.font = [UIFont systemFontOfSize:15];
self.nameLabel.textColor = [UIColor colorWithRed:0.18 green:0.18 blue:0.18 alpha:1.00];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30);
make.top.bottom.mas_offset(0);
make.left.mas_offset(10);
make.right.mas_offset(-10);
}];
} …Run Code Online (Sandbox Code Playgroud) TLDR;根据部分,我需要调整单元格的大小:
我只是在寻找:“是的,您可以通过组合布局来实现这一点,这就是如何......”。或者“不,你不能用组合来实现这个,你必须使用流布局或其他一些自定义布局。”
几个额外的要求:
UITableViewAutomaticDimension背景资料:
dynamic ios uicollectionview swift uicollectionviewcompositionallayout
ios ×5
swift ×4
autolayout ×2
objective-c ×2
uitableview ×2
dynamic ×1
ios12 ×1
ios6 ×1
reloaddata ×1