- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个整数数组,我试图将数组中所有行的 UITableViewCell 高度设置为 50,并将其余行设置为 0。使用 Swift 3,我的数组返回 [0,1,3,6]
并且我使用 for 循环遍历数组中的所有元素。
在我的 heightForRowAt indexPath
函数中,我将 indexPath.row
与这些值进行比较并适本地设置高度。但是,它只适用于第一个元素,而不适用于所有元素。
简单的写代码:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isSearching == true{
searchBarIndexs.sorted(by: {$0 < $1})
for allvalues in searchBarIndexs{
if indexPath.row == allvalues{
return 52
} else {
return 0
}
}
} else {
return 52
}
return 52
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching{
return searchBarIndexs.count
} else {
return usernames.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! usersProfileTableViewCell
cell.userUsernameLabel.text = usernames[indexPath.row]
cell.userIdLabel.text = userIDs[indexPath.row]
self.allimages[indexPath.row].getDataInBackground { (data,error) in
if let imageData = data {
if let downloadedImage = UIImage(data: imageData){
cell.profileImage.image = downloadedImage
}
}
}
return cell
}
我缺少什么逻辑以便为数组中的所有元素而不只是第一个元素适本地设置高度?我尝试过使用 indexPath.contains 的其他方法,但无济于事。
最佳答案
不要循环。只需检查当前的 indexPath
。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isSearching {
return searchBarIndexs.contains(indexPath.row) ? 52 : 0
} else {
return 52
}
}
关于swift - 将多行的 heightForRowAt indexPath 设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50754263/
使用此代码,我根据数组中的项目数设置单元格的自动尺寸。它可以工作,但是当我使用另一个 viewController 添加一些新项目然后返回到此时,即使数组数量增加,高度也不会更新。 我在viewDid
像往常一样,我尝试使用以下代码向某些单元格添加一些自定义高度值: func tableView(_ tableView: UITableView, heightForRowAt indexPath:
我正在开发一个电子商务应用程序,但在尝试设置特定行的高度时遇到了问题。当我选择类别时,表格会重新加载,并且根据我选择的类别,或多或少的行会通过 api。 因此,使用此设置,当我选择一个特定类别时它可以
我有一个带有自定义 UITableViewCell 的 UITableViewController。每个单元格有 2 个标签。当单元格被选中时,它会扩展到一个固定值,我通过 tableView(_ t
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if le
我正在尝试根据下载图像的大小实现动态大小的行高。我遇到的问题是当函数 heightForRowAt 运行时图像没有被下载。实现此代码的正确方法是什么。 images是UIImage的数组,rowHei
我正在研究 UI 以使其与设计相同,我使用 heightForRowAt 将第一部分的单元格高度设置为 96px,我尝试了: override func tableView(_ tableView:
我正在使用 UITableViewController 并将 UICollectionView 放在 TableView 单元格的第二行中。问题是我如何动态获取 heightForRowAt 。 我尝
我有一个 View Controller ,它使用名为 searchCell 的自定义单元格来引入内容,我想知道如何获取标签 lblLeft 的高度并在 tableView(_ tableView:
什么时候设置改变 heightForRowAt UITableView 动画是 broken cell 是一个跳跃。如果选择最后一行并滚动到顶部,然后点击折叠行,表格会向上跳。动画坏了。 overri
通常当我想设置行高时再设置 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFl
为什么函数 heightForRowAt 总是找到包含行高的数组,该数组最初设置为 nil,即使在重新加载行后仍然为零。 override func tableView(_ tableView
我有一个整数数组,我试图将数组中所有行的 UITableViewCell 高度设置为 50,并将其余行设置为 0。使用 Swift 3,我的数组返回 [0,1,3,6] 并且我使用 for 循环遍历数
我在一个 ViewController 中有 2 个 tableview。两个表格 View 的单元格高度不同。 在第一个表格 View 中,我通过 heightForRowAt 委托(delegat
我尝试为动态内容设置单元格高度。在单元格中,我使用了两个 UILabel,但我无法从 tableView(_ tableView: UITableView, heightForRowAt indexP
我正在更改每个 TableViewCell 的大小: override func tableView(_ tableView: UITableView, heightForRowAt indexPat
我有一个带有 UITableView 的 View Controller 。使用 RxSwift 填充表数据: let observable = Observable.just(data) obser
每当我重新加载 tableView 时,heightForRowAt 都会在 cellForRowAt 之后调用。它遵循的顺序是: numberofsections -> numberOfRowsI
我对编码还很陌生,所以尝试理解这些概念。 这里我试图获取具有动态单元格高度的 UITableView 的高度。 这是我的代码部分。 override func viewWillAppear(_ ani
我的tableView设置 appTableView.estimatedRowHeight = 200 appTableView.rowHeight = UITableViewAutomaticDim
我是一名优秀的程序员,十分优秀!