gpt4 book ai didi

ios - UITableview 滚动没有正确响应?

转载 作者:行者123 更新时间:2023-12-01 19:44:03 27 4
gpt4 key购买 nike

 booktable.frame = CGRect(x: 0, y: booktopview.bounds.height, width: screenWidth, height: screenHeight-booktopview.bounds.height-tabbarView.bounds.height)

booktable.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
booktable.dataSource = self
booktable.delegate = self
booktable.separatorColor = UIColor.lightGray
booktable.backgroundColor = UIColor.clear
booktable.separatorStyle = .singleLine

bookview.addSubview(booktable)



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if(tableView == booktable)
{
let cell1 = booktable.dequeueReusableCell(withIdentifier: "mycell")

for object in (cell1?.contentView.subviews)!
{
object.removeFromSuperview();
}

let img :UIImageView = UIImageView()
let lbl : UILabel = UILabel()


img.frame = CGRect(x: 15, y: 15, width: 80, height: 130)
img.image = imgarray[indexPath.row]
img.layer.borderWidth = 1.0
img.layer.borderColor = UIColor.lightGray.cgColor
cell1?.contentView.addSubview(img)

imgheight = img.bounds.height

lbl.frame = CGRect(x: img.bounds.width + 40, y: (imgheight+40-80)/2, width: booktable.bounds.width-img.bounds.width + 40 - 100, height: 80)
lbl.text = imgname[indexPath.row]
lbl.numberOfLines = 0

lbl.textAlignment = .left
lbl.font = UIFont(name: "Arial", size: 23)
lbl.textColor = UIColor.black
cell1?.selectionStyle = .none



cell1?.contentView.addSubview(lbl)

return cell1!
}

上面显示的代码是用于书 table 的,它有时像正常滚动,有时根本不滚动。我正在以编程方式编写所有代码。我已经在模拟器和设备上对此进行了测试,但问题仍然存在。任何帮助表示赞赏...

最佳答案

创建自定义 UITableViewCell ,假设它是ListTableCell

class ListTableCell: UITableViewCell {

@IBOutlet weak var lblTemp: UILabel!
@IBOutlet weak var imgTemp: UIImage!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}
}

我创建了 UITableViewCellxib像这样绑定(bind) IBOutlets
enter image description here

假设我们有 struct Modelarray像这样
struct Model {
let image : UIImage
let name: String
}

for i in 0...10 {
let model = Model(image: #imageLiteral(resourceName: "Cat03"), name: "Temp \(i)")
array.append(model)
}

现在开始 ViewController viewDidLoad()方法,
tableView.register(UINib(nibName: "ListTableCell", bundle: nil), forCellReuseIdentifier: "ListTableCell")

实现 UITableViewDataSource像这样的方法,
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell") as! ListTableCell
let model = array[indexPath.row]
cell.lblTemp.text = model.name
cell.imgTemp.image = model.image
return cell
}

仅供引用

对于不同的tableviews,你可以用同样的方法创建不同的自定义单元格 cellForRowAt indexPathnumberOfRowsInSection方法会适当改变。

如有任何疑问,请告诉我。

更新

关注 thisthis以编程方式创建 CustomTableCell

关于ios - UITableview 滚动没有正确响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50603777/

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