简体   繁体   English

一键单击,多个操作TableView的自定义单元格

[英]One click, multiple actions Custom Cell of TableView

What I do 我所做的

I have a created a custom cell which has a button with a specific image on it. 我创建了一个自定义单元,其中带有一个带有特定图像的按钮。 Now depending on some conditions I set that image image1.jpg or image2.jpg . 现在,根据某些条件,我将图像设置为image1.jpgimage2.jpg

How I do that 我该怎么做

The custom cell is created by creating a protocol where I click that buttons action, I notice in the main interface, that the button is pressed and I pass as a parameter the button instance. 通过创建一个协议创建自定义单元,在该协议中,我单击该按钮动作,在主界面中注意到,该按钮已按下,并且将参数作为参数传递给实例。

Issue 问题

Now, the problem is when I click that button in a specific cell, I change cell.button 's image, but also with that, when I scroll down, I see that there are other cells that have changed their image . 现在的问题是,当我单击特定单元格中的该按钮时,我更改了cell.button的图像, cell.button ,当我向下滚动时, 我看到还有其他单元格也更改了其图像

Any idea why is this happening? 知道为什么会这样吗?

Due to the reuse of cells by the UITableView, when the cell moves out of the screen, it is moved to a queue and then reused when there are new cells to load. 由于UITableView对单元的重用,当单元移出屏幕时,它将移到队列中,然后在有新单元要加载时重用。

If you want to keep each cell's property unique, you will have to reconstruct its values every time. 如果要保持每个单元格的属性唯一,则每次都必须重新构造其值。 For that you can keep some kind of data array that stores the state/condition of each cell. 为此,您可以保留某种存储每个单元格状态/条件的数据数组。 The array will be ordered by the indexPath of the tableView, and then you can just fetch the state from the array based on the indexPath and update the cell to be the right image. 数组将由tableView的indexPath排序,然后您可以根据indexPath从数组中获取状态并将单元格更新为正确的图像。

What is happening is that when you change the image of your cell and later scroll down or up, your cell is being reused to present another information. 发生的情况是,当您更改单元格的图像并随后向下或向上滚动时,您的单元格将被重用于显示其他信息。
To fix that you should check your UITableView data source, see if you're reusing cells in this method: 若要解决您应该检查UITableView数据源的问题,请查看是否在这种方法中重复使用了单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


I'd recommend to configure the cell before returning it in the previous method, and assign the appropiate image for the button at that time. 我建议先配置单元格,然后再使用前面的方法返回它,然后为该按钮分配适当的图像。
To achieve that you'd need some mechanism for remembering the state of each cell. 为此,您需要某种机制来记住每个单元的状态。

For more information about cell reusing and cell in general you could go to the Apple Docs for UITableViewCell 有关单元重用和单元的一般信息,您可以转到Apple文档UITableViewCell

As described in above answer you can manage button selected and unselected state in array for that you can even add one key in the array that you are using for the data to display. 如上面的答案中所述,您可以管理数组中按钮的选择状态和未选择状态,甚至可以在用于显示数据的数组中添加一个键。

and on didselect event of table you can change the key of selected field like this 在表的didselect事件上,您可以像这样更改所选字段的键

 NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
            NSDictionary *oldDict = self.dict_trans_Details ;
            [newDict addEntriesFromDictionary:oldDict];
            [newDict setObject:status forKey:@"transactionstatusid"];
            [table_transdetails_array replaceObjectAtIndex:self.indexValue withObject:newDict];

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM