Ios UITableview单元格保留自定义图像视图过滤器

Ios UITableview单元格保留自定义图像视图过滤器,ios,objective-c,iphone,uitableview,custom-cell,Ios,Objective C,Iphone,Uitableview,Custom Cell,在开发tableview自定义单元格时,我经常遇到这个问题 这里有一个问题,我有一个tableview,它有很多自定义的单元格,当用户点击任何一个单元格推送新的UIViewController,用户填充一些数据并点击SaveViewController推送委派方法时,它有UIImageView和UILabel 在这个委托方法中,我检查抽头单元格,并像选择状态一样更改该着色颜色,但我只更改自定义imageview着色颜色。所以这会正确地改变,但当我滚动任何垂直方向时,着色颜色都会消失。下面的图片和

在开发tableview自定义单元格时,我经常遇到这个问题

这里有一个问题,我有一个tableview,它有很多自定义的单元格,当用户点击任何一个单元格推送新的UIViewController,用户填充一些数据并点击SaveViewController推送委派方法时,它有UIImageView和UILabel

在这个委托方法中,我检查抽头单元格,并像选择状态一样更改该着色颜色,但我只更改自定义imageview着色颜色。所以这会正确地改变,但当我滚动任何垂直方向时,着色颜色都会消失。下面的图片和正确计算的代码

当从委托弹出查看控制器时,方法正常工作

滚动垂直方向三角网时


当选中该单元格时,跟踪其索引路径,而不是单元格本身,滚动时会重复使用该单元格。为匹配的选定索引路径显示单元格时,以及返回视图时,在cellForRow中应用选定样式

更新:添加代码以澄清

在自定义单元格中,提供启用/禁用着色的简单方法:

@interface CustomCell : UITableViewCell

@property(strong, nonatomic) UIImageView *imageView;
@property(strong, nonatomic) UILabel *titleLabel;

- (void)enableLastSelectedHighlight;
- (void)disableLastSelectedHighlight;

@end
实施:

@implementation CustomCell

- (void)prepareForReuse
{
    [super prepareForReuse];

    // Reset prior to being reused
    [self disableLastSelectedHighlight];
}

- (void)enableLastSelectedHighlight
{
    self.imageView.image = [cell.customImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.imageView.tintColor = [UIColor colorWithRed:0.27 green:0.58 blue:0.98 alpha:1];
}

- (void)disableLastSelectedHighlight;
{
    self.imageView.image = [cell.customImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end
在TableViewController.m文件中,通过didSelectRowAtIndexPath或现有自定义代理用户TAPPEDBACKBUTTON跟踪选择。该计划的实施将是相同的:

@interface MyTableViewController ()

@property (nonatomic, strong) NSIndexPath *lastSelectedCellIndexPath;

@end

@implementation MyTableViewController

// Your existing implementation
// ...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Update our reference to the tinted row
    [self setLastSelectedCellIndexPath:indexPath];

    // Un-tint any currently tinted cells
    [self.tableView.visibleCells makeObjectsPerformSelector:@selector(disableLastSelectedHighlight)];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get your cell
    CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    if (indexPath == self.lastSelectedCellIndexPath) {
        // This cell should be tinted
        [cell enableLastSelectedHighlight];
    }
    // The rest of your cell setup...
}

@end

您遇到的问题是,您正在保存对UITableViewCell的引用,但这些单元格正在被重用

您需要以另一种方式保存有关要高亮显示的单元格的信息,这种方式不受重复使用的单元格的影响。我建议使用indexath

类似于以下的方法应该可以工作:

@property (nonatomic, strong) NSIndexPath *lastSelectedIndexPath;


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

    CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    if([indexPath isEqual:self.lastSelectedIndexPath]) {
        cell.imageView.image = [cell.customImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        cell.imageView.tintColor = [UIColor colorWithRed:0.27 green:0.58 blue:0.98 alpha:1];
    }
    ...
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Save the selected indexPath
    self.selectedIndexPath =  indexPath;
}
- (void)userTappedBackButton {
    // Reload the row that needs to be updated with the new tint color
    [tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

如果成功了就告诉我。

谢谢你,伙计。这就是我需要的。但另一种方法是所有单元的懒散负载。但有时它会带来麻烦。我很想知道为什么,以及当我一次又一次地点击同一个单元格时,你最终得到了什么解决方案。我的数组变成了这样。为了应用程序性能,我认为这不是一种方便的方式。添加了代码示例以使其更清晰。听起来像是将其存储为一个数组-除非对多个数组进行着色,否则不需要。然后,可以在重新添加之前检查索引路径是否已经是着色索引路径数组的成员。或者我们的NSSet。我有多个节,所以我猜这段代码不起作用。您正在保存包含节和行信息的indexPath,因此它肯定可以处理更多节
@property (nonatomic, strong) NSIndexPath *lastSelectedIndexPath;


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

    CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    if([indexPath isEqual:self.lastSelectedIndexPath]) {
        cell.imageView.image = [cell.customImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        cell.imageView.tintColor = [UIColor colorWithRed:0.27 green:0.58 blue:0.98 alpha:1];
    }
    ...
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Save the selected indexPath
    self.selectedIndexPath =  indexPath;
}
- (void)userTappedBackButton {
    // Reload the row that needs to be updated with the new tint color
    [tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}