2015-04-22 95 views
0

我在开发tableview自定义单元格时经常遇到这个问题。我有一个tableview,它有很多自定义的单元格(一个UIImageView和UILabel)当用户点击任何这个单元格推新UIViewController和用户填充一些数据,并点击“保存”viewcontroller推回委托方式。UITableview单元格保留自定义图像视图过滤器

在此代理方法中,我检查点击单元格并更改该色调颜色(如选定状态,但我只更改自定义的ImageView色调颜色)。所以这个改变是正确的,但当我滚动任何垂直方向色调消失。下面的图片和代码正确计算出来。

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

enter image description here

当滚动垂直方向锡

enter image description here

// Custom cell 

@interface CustomCell : UITableViewCell 
@property(strong, nonatomic) UIImageView *imageView; 
@property(strong, nonatomic) UILabel *titleLabel; 
@end 

// Custom Cell implementation nothing special here. 

// UIViewController delegate method when pop back 
// I'm filling specific color 

@interface UIViewController 
@property (strong,nonatomic) CustomCell *myCustomCell; 
@end 

@implementation UIViewController 



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

    _myCustomCell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath]; 

    ... 
} 

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

回答

0

跟踪吨的他会在被选中时指定被点击的单元格的路径,而不是单元格本身,当您滚动时会重新使用该单元格。在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或您现有的自定义代理userTappedBackButton跟踪选择。的实施将是相同的:

@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 
+0

谢谢老兄。这就是我需要的。但另一种方法是所有单元的负载都很低。但有时会造成麻烦。 –

+0

经过一些测试后,我发现这不是一个好的选择。 –

+0

我很想知道为什么,以及你最终得到什么解决方案 – bdalziel

0

你的问题是,你是保存到UITableViewCell一个参考,但在细胞被重用。

您需要以另一种方式保存有关要突出显示的单元格的信息,这种方式不会受到正在重用的单元格的影响。我会建议通过使用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]; 
} 

让我知道,如果它的工作。

+0

我有多个部分,所以我想这个代码将无法正常工作。 –

+0

您正在保存同时具有分区和行信息的indexPath,因此它将与更多分区一起工作 –

+0

不,它不适用于许多行和分区。 –