向自定义单元格视图添加控件 [英] adding a control to a custom cell view

查看:29
本文介绍了向自定义单元格视图添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试解决这个问题时感到非常沮丧,我尝试以多种方式实现它,但没有解决方案.我在自定义单元格中有一个 UIStepper,我想更改单元格上的值.一切正常,当我滚动 tableView 时,UIStepper 中的值从一个单元格更改为另一个单元格.请帮忙看看我的屏幕截图.
tableview 实现链接链接到单元格实现

I am getting really frustrated trying to solve this problem i tried implementing it in many many ways but no solution. I have a UIStepper in a custom cell and i want to change a value on the cell. Everything works fine expect when i scroll around the tableView the values from the UIStepper changes from one cell to another. Please help here are my screen shoots.
Link to tableview implementation and link to cell implementation

推荐答案

您正在尝试将步进值存储在每个单独的单元格中.这是行不通的,因为细胞被重用了;当用户滚动时,您现在在第 2 行看到的单元格可能会再次出现在第 20 行.

You are trying to store the stepper value inside each individual cell. That's not going to work because cells are reused; the cell that you now see in row 2 may reappear in row 20 when the user scrolls.

这就是为什么您必须将步进器的值逐行存储在模型(您的数据)中,以便您可以每隔单次 cellForRowAtIndexPath: 被调用.

That is why you must store the value for the stepper in the model (your data) on a row-by-row basis, so that you can set it freshly and correctly for that row every single time cellForRowAtIndexPath: is called.

这反过来意味着当用户步进步进器时,它的 valueChanged 将需要与表视图数据源进行对话,以便模型可以被更新(第 5 行的步进值刚刚更改为 3")并在模型中维护.

This, in turn, means that as the user steps the stepper, its valueChanged is going to need to talk to the table view data source so that the model can be updated ("the stepper value for row 5 has just been changed to 3") and maintained in the model.

这篇关于向自定义单元格视图添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆