gpt4 book ai didi

ios - 表格 View 单元格高度

转载 作者:行者123 更新时间:2023-12-01 18:39:40 24 4
gpt4 key购买 nike

我的表格 View 中有多个单元格;例如猫细胞、狗细胞、鸡细胞。在 Cat Cell 中,我有两个 View :图形 View 和 ImageView 。

如果图形 View 被隐藏而 ImageView 可见,那么我想将单元格高度设置为 200,在相反的情况下设置为 350。

我将如何实现这一目标?我正在 heightForRowAt 中注册我的 nib 文件 TableView 的委托(delegate)方法,我尝试了不同的方法无济于事。这是我单元格中的两个 View :

@property (weak, nonatomic) IBOutlet UIView *imagesView;
@property (weak, nonatomic) IBOutlet UIView *graphView;

...这是我的 heightForRowAt方法:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];
if (cell.isGraphViewOnTop == YES) {
return 350;
}
else {
return 200;
}
return 200;
}

最佳答案

您需要访问 tableView 中已有的内容,而不是启动新单元格。 .

代替

TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];


TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath];

而不是看看属性(property)。

编辑:
看看我们的冗长讨论,这里有一个非常可靠的例子,说明你可以在 Swift 中使用一种方法。
import UIKit

class RightAlignedCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subLabel: UILabel!
// Your boolean property you would like to map
var randomBool: Bool = false
}

class RightAlignedTableViewController: UIViewController {
// This is just an example for the dataSource, you should have this somewhere already
lazy var dataSource: [Bool] = {
var dataSource: [Bool] = []
for index in 0..<10 {
dataSource.append(index % 2 == 0)
}
return dataSource
}()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
}

extension RightAlignedTableViewController: UITableViewDelegate {}

extension RightAlignedTableViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Take a look in the !!!dataSource!!!, what is value for isHidden
let isHidden = self.dataSource[indexPath.row]
// return the right height
return isHidden ? 60 : 100
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: RightAlignedCell = tableView.dequeueReusableCell(withIdentifier: "RightAlignedCell", for: indexPath) as! RightAlignedCell
cell.titleLabel.text = "Something"
cell.subLabel.text = "Nothing"
cell.randomBool = self.dataSource[indexPath.row]

return cell
}
}

关于ios - 表格 View 单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345065/

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