iPhone:调用 reloadSections 后 UITableView 部分中的单元格顺序发生变化:方法
我有一张有两个部分的桌子。第一部分中的分段控件会更改第二部分中显示的行。我的问题是,每次随后按下分段控件中的按钮时,行的顺序以及第二部分中显示的行都会不正确地移动。
我允许用户通过 3 种不同的方式将产品添加到购物清单:按名称、按条形码和用相机拍照。我在 UISegmentedControl
中有 3 个按钮,以便用户可以选择使用哪种方法。根据用户选择的段,第二段中的字段应更改以显示与该方法相关的单元格。
Section 0:
0 row with segmented control showing name, barcode and camera buttons
Section 1:
// button zero, name button
0 row with textfield
1 row with textfield
or
// button 1, barcode button
0 row with textfield
or
// button 2, camera button
// shows camera view
我在每个 UITextField 中放置了占位符。
每次单击分段控件中的按钮时,我都会调用更新 tablevew 的 pickOne:
方法。在该方法中,我构造了一个 NSRange 为 (1, 1) 的 NSIndexSet,然后以 NSIndexSet 作为参数调用 UITableViewController 的 reloadSections:
方法。
当视图第一次出现时,一切正常,但是当我重复单击按钮时,单元格的顺序发生变化。包含 Button0 和新占位符的两个文本字段的单元格将覆盖旧的文本字段。 更糟糕的是,有时当我单击按钮 0 时,它只显示两个单元格中的第二个单元格。
我的详细代码可以在这里看到 http://pastebin.com/9GwMpCS9
I have a table with two sections. A segmented control in first sections changes which rows are displayed in the second section. My problem is that the order of the rows and which row are displayed in the second section shifts improperly upon each subsequent press of a button in the segmented control.
I allow a user to add a product to a shopping list 3 different ways: by name, by barcord and by taking a picture with a camera. I have 3 buttons in a UISegmentedControl
so the users can select which method to use. Depending on which segement the user selects the fields in the second segment should change to show cells relevant to that method.
Section 0:
0 row with segmented control showing name, barcode and camera buttons
Section 1:
// button zero, name button
0 row with textfield
1 row with textfield
or
// button 1, barcode button
0 row with textfield
or
// button 2, camera button
// shows camera view
I've put placeholders in each UITextField.
Each time a button in the segmented control is clicked, I call a pickOne:
method that updates the tablevew. In that method, I construct a NSIndexSet with NSRange of (1, 1), and then I call the reloadSections:
method of the UITableViewController with the NSIndexSet as a parameter.
When the view appears for the first time, everything is ok but when I click the buttons repeatedly, the order of the cells changes. Cells containing the two textFields for the button0 and the new placeHolders are written over the old ones.
Worse, sometimes when I click on button 0, it shows me only the second cell of the two cells.
My detailed code can be seen here http://pastebin.com/9GwMpCS9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到了一些问题。
第一个大问题是您绕过
contentView
将子视图添加到单元格中。预定义样式的子视图根据其角色分为不同的部分。您拥有编辑控件、内容视图和附件视图。虽然您可以直接添加到单元格的视图,但会出现奇怪的行为,因为预定义的单元格期望内容位于内容视图中。我认为导致您问题的原因是您每次装饰单元格时都会添加子视图,但从未删除它们。当一个单元出队时,不能保证所有内容都恢复到原始的新状态,就像它被分配一样。诸如未删除的自定义附件视图之类的内容可以保留下来。我很确定这正在发生。您正在本应干净的单元格上收集视觉垃圾。
I'm seeing a couple of problems.
The first big one is that you're adding subviews into the cells bypassing the
contentView
. Subviews in predefined styles are broken up into different parts depending on their roles. You have the editing control, the content view, and the accessory view. While you can add directly to the cell's view, there'll be odd behavior because the predefined cells are expecting the content to be in the content view.I think what's causing your problem is that you're adding subviews every time a cell is decorated but you never remove them. When a cell is dequeued there's no guarantee that everything is restored to the pristine new condition as if it was alloc'ed. Things like custom accessory views that aren't removed can be left behind. I'm pretty sure that's happening. You're collecting visual trash on cells that should be clean.
我相信你的问题就在这里。
这是因为表始终显示有两个部分,因此始终为第 1 部分调用此方法。无论选择哪种输入类型,它都会创建一个单元格或将其出列并返回它。每当 addMode==SCAN 时,它都会随机地将先前使用的名称或条形码 addMode 的单元格之一出列并返回该单元格。
我建议您从表中完全删除 SCAN 逻辑,或者为相机创建一行。
我认为后者是最好的用户界面。通过前两个按钮,用户可以在第二部分中进行选择。您应该在选择相机时保持这种模式。只需有一个显示调用相机按钮的单元格即可。是的,它添加了第二个步骤,但为用户建立了一个动力学模式:在第一部分中选择输入类型,然后在第二部分中选择适当的单元格。用户不必每次都停下来思考是否需要点击第二部分中的某一行。他们应该自动这样做。
I believe your problem is here.
This because the table always shows has having two sections, this method is always called for section 1. Regardless of the input type selected, it creates or dequeue a cell and returns it. Whenever addMode==SCAN, it randomly dequeues one of the previously used cells for the name or barcode addMode and returns that.
I suggest that you remove the SCAN logic from the table altogether or that you create a row for the camera.
I think the latter the best UI. With the first two buttons, the users is presented with a choice in the second section. You should maintain that pattern with the camera choice. Just have a cell that displays a button that evokes the camera. Yes, it adds a second step but establishes a kinetic pattern for the user: Select input type in section one then select an appropriate cell in section two. The user shouldn't have to stop and think each time whether they need to hit one of the rows in section two or not. They should just do so automatically.