繁体   English   中英

UITableview滚动没有正确响应?

[英]UITableview scrolling is not responding properly?

 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!
    }

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

创建自定义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
    }
}

我已经用xib这样创建了UITableViewCell并绑定了IBOutlets

在此处输入图片说明

假设我们有这样的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
}

FYI

对于不同的表视图,您可以以相同的方式创建不同的自定义单元格,并且cellForRowAt indexPathnumberOfRowsInSection方法将适当地更改。

如有任何疑问,请通知我。

UPDATE

按照这个这个编程创建CustomTableCell

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM