UITableViewCell,两个单元格都获得了附件视图(UITableViewCell, two cells are both getting an accessory view)

编程入门 行业动态 更新时间:2024-10-26 02:28:22
UITableViewCell,两个单元格都获得了附件视图(UITableViewCell, two cells are both getting an accessory view)

情况:当用户选择单元格时,会向该单元格添加一个按钮。 当他们选择一个新单元格时,该按钮将从前一个单元格中删除并添加到新单元格中。 这样可行。 问题是当更多数据添加到表中时。 所以假设有20个单元格,然后我再添加20个单元格。 然后我选择第一个单元格,但是按钮被添加到单元格1 AND单元格21.选择委托方法仅注册被选择的第一个单元格。

从我的didSelectRowAtIndexPath方法:

if (self.selectedCell) { self.selectedCell.accessoryView = nil; self.selectedCell = nil; } UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [downloadButton setTitle:@"Download" forState:UIControlStateNormal]; [downloadButton setFrame:CGRectMake(0, 0, 130, 34)]; self.selectedCell = [self.tableView cellForRowAtIndexPath:indexPath]; self.selectedCell.accessoryView = downloadButton; [self.selectedCell setNeedsDisplay];

在我的方法中,我向表格添加了更多数据:

if(self.selectedCell){ self.selectedCell.accessoryView = nil; self.selectedCell = nil; } [self.tableView reloadData]; [self.tableView setNeedsLayout];

Situation: When a user selects a cell, a button is added to that cell. When they select a new cell, the button is removed from the previous cell and added to the new cell. That works. The problem is when more data is added to the table. So lets say there are 20 cells, then I add another 20 cells. I then select the first cell, however the button is added to cell 1 AND cell 21. The select delegate method only registers the first one being selected though.

From my didSelectRowAtIndexPath method:

if (self.selectedCell) { self.selectedCell.accessoryView = nil; self.selectedCell = nil; } UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [downloadButton setTitle:@"Download" forState:UIControlStateNormal]; [downloadButton setFrame:CGRectMake(0, 0, 130, 34)]; self.selectedCell = [self.tableView cellForRowAtIndexPath:indexPath]; self.selectedCell.accessoryView = downloadButton; [self.selectedCell setNeedsDisplay];

In my method that adds more data to the table I end with:

if(self.selectedCell){ self.selectedCell.accessoryView = nil; self.selectedCell = nil; } [self.tableView reloadData]; [self.tableView setNeedsLayout];

最满意答案

细胞被重复使用。 在你的cellForRowAtIndexPath: ,你忘了清除accessoryView 。 当细胞被重复使用时, accessoryView视图会出现。

我喜欢的一种技术是在tableView:willDisplayCell:forRowAtIndexPath:设置accessoryView tableView:willDisplayCell:forRowAtIndexPath: . 在将单元格放在屏幕上之前调用它。 然后你可以这样做:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (cell.isSelected) { cell.accessoryView = self.downloadButton; // No reason to create a new one every time, right? } else { cell.accessoryView = nil; } }

Cells are reused. In your cellForRowAtIndexPath:, you're forgetting to clear accessoryView. When the cell is reused, the accessoryView comes along.

A technique I like is to set the accessoryView in tableView:willDisplayCell:forRowAtIndexPath:. This is called right before the cell is put on the screen. You can then do something like:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (cell.isSelected) { cell.accessoryView = self.downloadButton; // No reason to create a new one every time, right? } else { cell.accessoryView = nil; } }

更多推荐

本文发布于:2023-08-07 10:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   单元格   获得了   附件   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!