简体   繁体   English

TableViewCell中的更改立即生效

[英]Change in TableViewCell to take effect immediately

I have a TableView with a custom cell. 我有一个带有自定义单元格的TableView。 One of the subviews in the cell is a UIButton. 单元格中的一个子视图是UIButton。 When a user clicks on the button, I want the background to change. 当用户点击按钮时,我希望更改背景。 I get all of that working. 我得到了所有这些。 But the problem is I cannot see the change until after I scroll the affected cell off screen and then return it on screen. 但问题是,在我将受影响的单元格从屏幕上滚动然后在屏幕上返回之前,我看不到更改。 But I want to see the change immediately, without the onscreen offscreen bit. 但我想立即看到变化,没有屏幕上的屏幕外位。 How might I do that? 我怎么能这样做?

For a bit more about my implementation: 有关我的实现的更多信息:

Inside the method (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I have the line 在方法内部(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我有这条线

....
[cell.myBtn addTarget:self action:@selector(onMyBtnTapped:) forControlEvents:UIControlEventTouchUpInside];

And then inside the onMyBtnTapped method is where I effect the color change. 然后在onMyBtnTapped方法中,我会影响颜色变化。

So perhaps what I need to do is to redraw a specific cell from the parent view controller (?). 所以我可能需要做的是从父视图控制器(?)重绘一个特定的单元格。

a bit more 多一点

I have gotten as far as getting the cell using [self.tableView cellForRowAtIndexPath:indexPath]; 我已经使用[self.tableView cellForRowAtIndexPath:indexPath];获取单元格了[self.tableView cellForRowAtIndexPath:indexPath]; . But now that I have the cell, I don't know how to get it to redraw itself. 但是现在我有了这个单元格,我不知道怎么让它重绘自己。 I do this on android all the time. 我一直在android上做这个。 I am not sure how to do it on iOS. 我不知道如何在iOS上做到这一点。

您可以在按钮单击时重新加载UITableViewCell:

[self.tableView reloadRowsAtIndexPaths:@[indexPathOfGivenCell] withRowAnimation:UITableViewRowAnimationNone];

You should be able to place the following in the method. 您应该能够在方法中放置以下内容。

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

You can ask to redraw cell at visible rows: 您可以要求在可见行重绘单元格:

NSArray *visibleIndexPaths       = [tableView indexPathsForVisibleRows];
[self.tableView reloadRowsAtIndexPaths:visibleIndexPaths withRowAnimation: UITableViewRowAnimationFade];

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

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