简体   繁体   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!
    }

The code shown above is for book table, which sometimes scrolls like normal and sometimes not scrolling at all. 上面显示的代码用于书本桌子,它有时像普通纸一样滚动,有时根本不滚动。 I am doing all the code programatically. 我正在以编程方式执行所有代码。 I have tested this on both simulators and devices but still the problem exists. 我已经在模拟器和设备上对此进行了测试,但是仍然存在问题。 Any help is appreciated... 任何帮助表示赞赏...

Create Custom UITableViewCell , let's say it is ListTableCell 创建自定义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
    }
}

I've created UITableViewCell with xib like this and bind IBOutlets 我已经用xib这样创建了UITableViewCell并绑定了IBOutlets

在此处输入图片说明

Let's say we have struct Model and array like this 假设我们有这样的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)
}

Now on ViewController viewDidLoad() method, 现在在ViewController viewDidLoad()方法上,

tableView.register(UINib(nibName: "ListTableCell", bundle: nil), forCellReuseIdentifier: "ListTableCell")

Implement UITableViewDataSource methods like this, 实现这样的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 FYI

For different tableviews, you can create different custom cell the same way and cellForRowAt indexPath and numberOfRowsInSection method will change appropriately. 对于不同的表视图,您可以以相同的方式创建不同的自定义单元格,并且cellForRowAt indexPathnumberOfRowsInSection方法将适当地更改。

Let me know in case of any queries. 如有任何疑问,请通知我。

UPDATE UPDATE

Follow this and this to create CustomTableCell programmatically 按照这个这个编程创建CustomTableCell

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

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