原型 tableView 单元格中的滚动问题

Posted

技术标签:

【中文标题】原型 tableView 单元格中的滚动问题【英文标题】:Issue with Scrolling in prototype tableView Cell 【发布时间】:2018-02-09 02:03:31 【问题描述】:

谁能解释为什么滚动视图会在.gif 中的某些单元格中选择步进器时自动向上滚动,这是在按下 2p 燕尾服时发生的(并非所有单元格都发生)下面是视图控制器 (Service1ViewController) 的代码,代码为单元格(Service1TableViewCell)和初始化函数(Service)的代码

我用提供的答案更新了代码,下面开始出现的这个问题是更新的代码以及更新的 .gif 文件。

[![更新问题][1]][1]

Service1ViewController

class Service1ViewController: UIViewController 

@IBOutlet weak var Service1IMG: UIImageView!
@IBOutlet weak var service1TableView: UITableView!

@IBOutlet weak var service1Total: UILabel!



var Dry: [Service1] = []

var orderCount = [Int : Int]()

override func viewDidLoad() 
    super.viewDidLoad()

    Dry = Options()
    for i in 0...30// TotaL number of rows
    
        orderCount[i] = 0 // DEFAULT order count will be 0
    




func Options() -> [Service1]

    var dryOptions: [Service1] = []

    let option1 = Service1(titled: "Pants", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option2 = Service1(titled: "Shirt", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option3 = Service1(titled: "Polo Shirt", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option4 = Service1(titled: "Polo (Long Sleeves)", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option5 = Service1(titled: "Shorts", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option6 = Service1(titled: "Tie", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option7 = Service1(titled: "2 Pc Tuxedo", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option8 = Service1(titled: "Jacket", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option9 = Service1(titled: "Men's 2pc Suite", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option10 = Service1(titled: "Blouse", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option11 = Service1(titled: "Skirt", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option12 = Service1(titled: "Women's 2pc Suite", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option13 = Service1(titled: "Dress", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option14 = Service1(titled: "Sweater", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option15 = Service1(titled: "Semi Formal Dress", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option16 = Service1(titled: "Customer Bag", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option17 = Service1(titled: "Jumpsuite", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option18 = Service1(titled: "Over Coat", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option19 = Service1(titled: "Rain Coat", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option20 = Service1(titled: "Skirt W/Pleats", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option21 = Service1(titled: "Robe", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option22 = Service1(titled: "Comforter /King", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option23 = Service1(titled: "Comforter /Queen", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option24 = Service1(titled: "Comforter /Twin", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option25 = Service1(titled: "Long Jacket", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option26 = Service1(titled: "Winter Jacket", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option27 = Service1(titled: "Men's 3pc Suite", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option28 = Service1(titled: "Women's 3pc Suite", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option29 = Service1(titled: "Overall", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option30 = Service1(titled: "Table Cloth/Small", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))
    let option31 = Service1(titled: "Shawl", pricing: 5.29, image: #imageLiteral(resourceName: "DryCleaning"))

    dryOptions.append(option1)
    dryOptions.append(option2)
    dryOptions.append(option3)
    dryOptions.append(option4)
    dryOptions.append(option5)
    dryOptions.append(option6)
    dryOptions.append(option7)
    dryOptions.append(option8)
    dryOptions.append(option9)
    dryOptions.append(option10)
    dryOptions.append(option11)
    dryOptions.append(option12)
    dryOptions.append(option13)
    dryOptions.append(option14)
    dryOptions.append(option15)
    dryOptions.append(option16)
    dryOptions.append(option17)
    dryOptions.append(option18)
    dryOptions.append(option19)
    dryOptions.append(option20)
    dryOptions.append(option21)
    dryOptions.append(option22)
    dryOptions.append(option23)
    dryOptions.append(option24)
    dryOptions.append(option25)
    dryOptions.append(option26)
    dryOptions.append(option27)
    dryOptions.append(option28)
    dryOptions.append(option29)
    dryOptions.append(option30)
    dryOptions.append(option31)

    return dryOptions




extension Service1ViewController: UITableViewDataSource,UITableViewDelegate
  @IBAction func stepperAcn(_ sender: UIStepper) 
    let cellPosition = sender.convert(CGPoint.zero, to: service1TableView)
    let indPath : IndexPath = service1TableView.indexPathForRow(at:    cellPosition)!

    orderCount[indPath.row] = Int(sender.value)

    service1TableView.beginUpdates()
    service1TableView.reloadRows(at: [indPath], with: UITableViewRowAnimation.none)
    service1TableView.endUpdates()
    //service1TableView.reloadData()



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    return Dry.count




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let Dry1 = Dry[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "Service1Cell") as! Service1TableViewCell
    cell.setService1(Dry: Dry1)
    cell.quantityStepper.tag = indexPath.row
    cell.stepperLabel.text = String(orderCount[indexPath.row]!) // PASS VALUES TO LABEL
    cell.quantityStepper.value = Double(orderCount[indexPath.row]!) // PASS VALUES TO STEPPER

    cell.selectionStyle = .none

    return cell


// add function to collect (didSelectRowAt) and send selected data to cart
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)            




Service1TableViewCell

class Service1TableViewCell: UITableViewCell 


@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var quantityStepper: UIStepper!
@IBOutlet weak var Service1image: UIImageView!
@IBOutlet weak var serviceTitle: UILabel!
@IBOutlet weak var pricingInfo: UILabel!
@IBOutlet weak var stepperLabel: UILabel!



func setService1(Dry: Service1)
    Service1image.image = Dry.service1image
    pricingInfo.text = Dry.service1Pricing.description
    serviceTitle.text = Dry.service1Title


//save data from each cell and multiply for total service


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



服务

class Service

var title: String
var description: String
var image: UIImage



init(titled: String, description: String, image: UIImage)

    self.title = titled
    self.description = description
    self.image = image


【问题讨论】:

显示您的代码。 我用代码编辑的不好 在设备中试用。 模拟器和设备都试过了 如何为下面的图像和标签设置约束 【参考方案1】:

这个问题可能来自重新加载整个 TableView。让我们一起去reloadRows

@IBAction func stepperAcn(_ sender: UIStepper) 

    let cellPosition = sender.convert(CGPoint.zero, to: service1TableView)
    let indPath : IndexPath = service1TableView.indexPathForRow(at: cellPosition)!

    orderCount[indPath.row] = Int(sender.value)

    service1TableView.beginUpdates()
    service1TableView.reloadRows(at: [indPath], with: UITableViewRowAnimation.none)
    service1TableView.endUpdates()

    //service1TableView.reloadData()


【讨论】:

错误仍然存​​在,但没有以前那么糟糕,他们有什么方法可以为每个单元格的表格视图进行分页,所以当您将每个 cel 剪辑滚动到顶部约束时? 检查,你的编码中是否有其他 reloadData() 刚刚我是否删除了 service1TableView.reloadData(),现在当某些单元格位于屏幕中的某个位置时,下面的单元格会垂直向下反弹 cellPosition CGPoint (x = 266, y = 314) 这是发生新错误的时间

以上是关于原型 tableView 单元格中的滚动问题的主要内容,如果未能解决你的问题,请参考以下文章

在 tableview 单元格中显示图像

TableView 交互阻止单元格中的动画

在iOS中选择单元格时如何以编程方式更改原型单元格中的标签文本颜色和图像

滚动 UITableView 框架问题以查看单元格中的 UITextField?

滚动 tableView 后,视频在 AVPlayerController 的 UITableView 单元格中停止加载

表格单元格中的集合视图。这是重用的错误