gpt4 book ai didi

ios - swift : Different UIView in each UITableView cell

转载 作者:行者123 更新时间:2023-11-29 01:16:42 25 4
gpt4 key购买 nike

我只是想让不同的 UIView 出现在每个单元格中。所以我这样做:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

cell.textLabel?.text = self.items[indexPath.row]

if(indexPath.row == 0){

print("Index path shold be 0 : \(indexPath.row)")

maleIcon = MaleIconView(frame: CGRect(x: 0, y: 0, width: 55, height: 55))
maleIcon.center.x = cell.center.x + cell.center.x * 0.5
maleIcon.center.y = cell.contentView.center.y
maleIcon.layer.cornerRadius = 0.5 * maleIcon.bounds.size.width
cell.addSubview(maleIcon)

}
else if(indexPath.row == 1){
//Next UIView(different from one above) will go here
}


return cell

}

我一共有12个细胞。 UIView 首先会出现在正确的单元格中,但是如果您继续向下滚动,相同的 UIView 将出现在不同的单元格中,然后向上滚动 UIView 将消失,从这里开始它就会遍布整个屏幕。拜托拜托请帮忙!我今天必须把这个应用程序推出!!

最佳答案

在你的数据源中添加 subview 太糟糕了。因为重用, View 会创建很多次。解决方案是在你的 Storyboard中制作两个具有不同标识符的类型单元格。然后在你的 Storyboard中添加 MaleIconView(它将调用 init (编码器:)函数,请覆盖该函数)。

另一个解决方案是在您的自定义单元格 prepareForReuse() 中删除 MaleIconView。您不需要为此创建新单元格。

我查看了您的代码。您应该在 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

中为您的单元格设置性别

更优雅的解决方案是使用静态 TableView ,这样会更容易。

关于ios - swift : Different UIView in each UITableView cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097407/

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