是否可以为细胞设定最小高度?我用动态:
tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)
但是当我news title label text在一行时,我需要为细胞设置最小高度.
我使用UITableView同estimatedRowHeight和UITableViewAutomaticDimension.我也用它NSFetchedResultsControllerDelegate来反映变化.
一切都很好.现在的问题是,当我添加一些记录时CoreData,NSFetchedResultsController称为它的委托,但意外的事情发生.每次TableView突然滚动到Top.
NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
break
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
break
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
break
default: break;
}
}
Run Code Online (Sandbox Code Playgroud)
通过谷歌搜索我发现很少有人建议使用的答案,tableView: heightForRowAtIndexPath:但因为我的细胞高度是动态的.所以我该怎么做?
core-data uitableview ios swift uitableviewautomaticdimension
我遇到了自动/动态UITableView节头视图的问题,其中包含一个包装的UILabel(numberOfLines = 0).高度未正确计算,尤其是在滚动表格并重新使用视图后.有时UILabel包裹,有时它被截断,有时一个或多个标签不可见,有时标签之间有额外的间距.自定义视图包含一个带有三个UILabel的垂直UIStackView,其中一个包装.
可以在https://github.com/outerstorm/tableviewHeaderTest找到演示该问题的完整示例应用程序.
截面标题高度在viewDidLoad中设置为自动,具有以下内容:
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 30.0
Run Code Online (Sandbox Code Playgroud)
并且还实现了以下heightForHeaderInSection,试图让它工作:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在各个点调用setNeedsLayout()和layoutIfNeeded()无济于事.任何建议将不胜感激.
以下是应用中看到的行为的屏幕截图.第一部分是截止的,第二部分太高了:
uitableview ios uitableviewautomaticdimension uitableviewsectionheader
为什么UITableView在滚动时会跳转?你能帮助我吗?
对于一个谁帮助我,我开始和获奖后ANOTHER 100 :-)的赏金
如何进行一些更改UITableView以及设置它contentOffset?
这是我设置我的方式scrollDisplayLink:
scrollDisplayLink = CADisplayLink(target: self, selector: Selector("scrollTable"))
scrollDisplayLink?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
cellForCurrentIndexPath?.hidden = true
Run Code Online (Sandbox Code Playgroud)
然后在scrollTable我做以下事情:
func scrollTable() {
var newOffset = CGPointMake(currentContentOffset.x, currentContentOffset.y + scrollRate * 10)
if currentContentSize.height < frame.size.height {
newOffset = currentContentOffset
} else if newOffset.y > currentContentSize.height - frame.size.height {
newOffset.y = currentContentSize.height - frame.size.height
} else if newOffset.y < 0 {
newOffset = CGPointZero
}
contentOffset = newOffset
if coveredIndexPath …Run Code Online (Sandbox Code Playgroud) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let json = JSON(data: activityTableView.onlineFeedsData)[indexPath.row] // new
if(json["topic"]["reply_count"].int > 0){
if let cell: FeedQuestionAnswerTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier_reply) as? FeedQuestionAnswerTableViewCell{
cell.selectionStyle = .None
cell.tag = indexPath.row
cell.relatedTableView = self.activityTableView
cell.configureFeedWithReplyCell(cell, indexPath: indexPath, rect: view.frame,key: "activityTableView")
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.mainScreen().scale
return cell
}
}else{
if let cell: FeedQuestionTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? FeedQuestionTableViewCell{
cell.selectionStyle …Run Code Online (Sandbox Code Playgroud) 我有一个自定义表格视图单元格,它使用自动布局,并有一个披露指标作为附件视图.首次显示时,屏幕上单元格的单元格大小完全错误:
正如您所看到的那样,该单元占用了大约1.5个屏幕的空间:

但是,如果我旋转设备并向后旋转它看起来很好:

正如你在这里看到的,我没有做过任何复杂的事情:

我有一个非常非理想的解决方法:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
但是当你第一次看到屏幕时,这显然会导致"闪光".在更复杂的情况下,闪光灯更加明显.
我有另一个解决方法,但这会导致自动布局异常:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
cell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines";
[cell.basicLabel layoutIfNeeded];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
例外:

至少这种方法不会给我UI闪烁.
如果我删除附件视图,它实际上完全正常.
更新:我已经向github添加了一个示例项目:https: //github.com/fwaddle/TableCellAccessoryTest
更新#2:关于这个错误的另一项工作是在代码中布局单元格.我只是尝试在代码中做同样的事情,它没有抛出警告并且工作正常.看起来像一个IB bug.
有任何想法如何解决这个问题?谢谢.
objective-c uitableview ios ios-autolayout uitableviewautomaticdimension
我正在按照本教程制作自定尺寸单元格.
我将自定义单元格注册到表格视图中,在单元格xib中,我手动给出了每个子视图约束.请参阅GitHub中的源代码
在一个单元格的内容视图中垂直排列3个标签在iOS 9中运行良好,但在iOS 8中运行良好(均在设备和模拟器中测试).
在iOS 8中,一个单元格没有拟合高度,并且并非每个标签都显示全部文本.
iOS 8不合适:
iOS 9正如我所料:
任何意见,将不胜感激!
更新
代码viewDidLoad:
tableView.registerNib(UINib.init(nibName: "SelfSizingCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
tableView.dataSource = self
tableView.delegate = self
tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension
tableView.reloadData()
Run Code Online (Sandbox Code Playgroud) uicollectionviewcell swift ios8 uitableviewautomaticdimension ios9
我有用于显示客户下的食品订单的 Tableview。其中包含 Horizontal UIStackview。在UIStackView
In UIStackview很少有一个/两个内衬标签,一个UITableView用于显示订单项。内部 tableview 具有固定的高度常量(大于或等于 + 750 优先级)并且将随着内容大小而改变。高度和滚动禁用。
内部 tableview 有两个标签。高度是自动尺寸
我希望主 tableview 单元格高度应根据内部 tableview 项目自动增加。所以我申请主tableview
self.tblOrders.rowHeight = UITableViewAutomaticDimension
self.tblOrders.estimatedRowHeight = 45
Run Code Online (Sandbox Code Playgroud)
和在 tableview 单元格子类中
override func awakeFromNib() {
super.awakeFromNib()
self.tblOrderMaster.dataSource = self
self.tblOrderMaster.delegate = self
self.tblOrderMaster.estimatedRowHeight = 45
self.tblOrderMaster.rowHeight = UITableViewAutomaticDimension
self.selectionStyle = .none
self.layoutIfNeeded()
self.tblOrderMaster.isScrollEnabled = false
}
Run Code Online (Sandbox Code Playgroud)
内部 tableview 数据源数组
var orderData:[ODOrderDataMaster] = [] {
didSet {
self.tblOrderMaster.reloadData()
self.layoutIfNeeded()
self.const_Height_tblOrderMaster.constant = self.tblOrderMaster.contentSize.height
self.layoutIfNeeded()
}
}
Run Code Online (Sandbox Code Playgroud)
题:当我滚动主 tableview 时,它没有按照子项正确调整大小。而最初它显示正确。 …
我有一个 tableview,其中填充了来自 Firebase 的数据。但是,在使用自动尺寸调整 tableview 的大小时,会显示一些文本被截断。
这是我的故事板,约束设置为顶部、底部、右侧和左侧。
当没有很多文本时,它工作正常,如图所示。
但是,当我用大量文本填充单元格时,就会发生这种情况。
这是我目前正在使用的代码。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (tableView == questInformationTableView) {
return 50
}
else if (tableView == questWalkthroughTableView) {
return UITableView.automaticDimension
}
return 0
}
override func viewDidLoad() {
super.viewDidLoad()
questWalkthroughTableView.estimatedRowHeight = 250
questWalkthroughTableView.rowHeight = UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Run Code Online (Sandbox Code Playgroud)
我还将 numberOfLines 设置为 0 并将标签设置为 .sizeToFit()
我很难通过自动调整单元格大小来实现 UITableView。我正在尝试使用 UIStackView,单元格内有 2 个带有自动大小的标签。
总体看起来不错,但我遇到了以下运行时问题:
我觉得我所拥有的约束应该足以满足这里的用例,但我想消除这里的歧义。
如何才能实现这一目标?这里采取的常见方法是什么?
我准备了一个示例项目,它简化了我的用例,但仍然演示了问题。这是我的控制器:
class ViewController: UIViewController {
struct CellData {
let title: String
let subtitle: String
}
let table = UITableView()
let data = [
CellData(title: "Foo", subtitle: "Bar"),
CellData(title: "Baz", subtitle: "FooBar")
]
private let cellReuseID = "CellReuseID"
override func viewDidLoad() {
super.viewDidLoad()
table.register(Cell.self, forCellReuseIdentifier: cellReuseID)
table.dataSource = self
table.rowHeight = UITableView.automaticDimension
table.tableFooterView = UIView(frame: .zero)
table.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(table)
NSLayoutConstraint.activate([
table.leadingAnchor.constraint(equalTo: view.leadingAnchor),
table.trailingAnchor.constraint(equalTo: view.trailingAnchor),
table.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
table.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
])
}
}
extension …Run Code Online (Sandbox Code Playgroud) uitableview uikit ios uitableviewautomaticdimension uistackview
uitableviewautomaticdimension ×10
ios ×8
uitableview ×8
swift ×7
ios8 ×2
core-data ×1
ios9 ×1
objective-c ×1
uikit ×1
uistackview ×1
xcode ×1