gpt4 book ai didi

swift - 将多行的 heightForRowAt indexPath 设置为 0

转载 作者:行者123 更新时间:2023-11-28 10:40:19 24 4
gpt4 key购买 nike

我有一个整数数组,我试图将数组中所有行的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com