UITableview滚动没有正确响应?

时间:2018-05-30 11:41:50

标签: ios xcode uitableview swift4

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

上面显示的代码适用于书籍表,有时会像平常一样滚动,有时根本不滚动。我正在以编程方式执行所有代码。我已经在模拟器和设备上测试了这个,但问题仍然存在。任何帮助表示赞赏...

1 个答案:

答案 0 :(得分:2)

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

我已经UITableViewCell创建xib,并绑定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
}

<强> FYI

对于不同的Google视图,您可以采用相同的方式创建不同的自定义单元格,cellForRowAt indexPathnumberOfRowsInSection方法也会相应更改。

如有任何疑问,请与我联系。

<强>更新

关注thisthis以编程方式创建CustomTableCell