【发布时间】:2018-06-08 06:01:30
【问题描述】:
我有一个整数数组,我试图将数组中所有行的 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 的其他方法,但无济于事。
【问题讨论】:
-
请edit您的问题并显示您完整的
heightForRowAt方法(作为文本)。 -
嗨 rmaddy,很抱歉,我已经正确缩进并包含了完整的 heightForRowAt 方法
标签: swift uitableview swift3 tableview heightforrowatindexpath