【问题标题】:UITableview scrolling is not responding properly?UITableview 滚动没有正确响应?
【发布时间】:2018-05-30 11:41:49
【问题描述】:
 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!
    }

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

【问题讨论】:

  • 对于 (cell1?.contentView.subviews) 中的对象! { object.removeFromSuperview(); } 这就是原因
  • 我有多个表格视图,所以如果我不使用它,内容会重叠,我应该怎么做?
  • 你应该创建自定义 UITableViewCell
  • 你能帮我提供一个示例代码吗

标签: ios xcode uitableview swift4


【解决方案1】:

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

仅供参考

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

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

更新

按照thisthis 以编程方式创建CustomTableCell

【讨论】:

  • 有没有办法不使用 xib 或故事板?
  • 是的,您也可以通过编程方式创建自定义单元
  • 我试图这样做,然后tableview overlaps的竞争,所以我使用了(cell1?.contentView.subviews)中的对象代码! { object.removeFromSuperview();它解决了所有 3 个表格视图中的问题,但仅在 booktable 上出现了滚动问题
  • 查看我的更新答案,有链接以编程方式创建单元格
  • 你应该扩展 UITableViewCell 和 init 方法,你可以添加子视图到单元格的内容视图,不需要删除所有子视图,因为它会重用 customcell 本身
猜你喜欢
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 2016-10-13
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多