2013-10-10 60 views
-1

在我的tableview中,每个单元格通过didselectRowAtIndexPath获取子视图以突出显示当前选定的行。一切正常,但在滚动tableview的时候,子视图不会从之前选择的单元中隐藏。将子视图添加到UITableViewCell以替换单元格选择

简而言之:如何替换“管理单元格选择和突出显示”?

在此先感谢!

这里是我的代码:

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 

@property (nonatomic,strong) NSArray *tableData; 

@end 

@implementation ViewController 

@synthesize checked_icon; 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    self.tableData = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T"]; 

} 

- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 
} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.tableData count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)]; 
     checked_icon.backgroundColor =[UIColor redColor]; 
    } 

    cell.textLabel.text = self.tableData[indexPath.row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow]; 
    if (currentSelectedIndexPath != nil) 
    { 
     [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor: [UIColor whiteColor]]; 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:currentSelectedIndexPath]; 

     if (cell.isSelected == YES) { 
      checked_icon.backgroundColor = [UIColor redColor]; 
     } 
     else { 
      checked_icon.backgroundColor = [UIColor clearColor]; 
     } 
    } 

    return indexPath; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor lightGrayColor]]; 
    UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:indexPath]; 
    [selectedcell.contentView addSubview:checked_icon]; 

} 


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (cell.isSelected == YES) { 
     [cell setBackgroundColor:[UIColor lightGrayColor]]; 

    } 

    else { 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
    } 
} 

@end 
+0

自定义的UITableViewCell? – Larme

回答

0

,您应该使用的UITableViewCell的“selectedBackgroundView”属性,选择会为你处理。

编辑

正确的方式做,这是创建的UITableViewCell的子类,并在那里有你的对勾参考。

虽然我的原始文章中的代码可以正常工作,但这并不是最好的方式,如果您的单元格视图变得更复杂,它将很快变得复杂。

ORIGINAL

如果你不想使用 'selectedBackgroundView',那么这也许应该解决您的问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     UIView *checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)]; 
     checked_icon.tag = 1234; 
     [cell.contentView addSubview:checked_icon]; 
    } 

    UIView *checked_icon = [cell.contentView viewWithTag:1234]; 
    // Note: color should be set each time a cell is presented 
    if (cell.isSelected) { 
     checked_icon.backgroundColor = [UIColor redColor]; 
    } 
    else { 
     checked_icon.backgroundColor = [UIColor clearColor]; 
    } 

    cell.textLabel.text = self.tableData[indexPath.row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 
+0

http://doing-it-wrong.mikeweller.com/2012/08/youre-doing-it-wrong-4-uiview.html – vikingosegundo

+0

我知道这不是一个很好的做法,我没有我不想让我的答案过长。更好的解决方案是拥有UITableViewCell的子类。 –

-1

感谢MrNickBarker!

viewWithTag似乎是唯一的解决方案...

下面是代码:(并不完美,但它的工作原理)

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 

@property (nonatomic,strong) NSArray *tableData; 

@end 

@implementation ViewController 

@synthesize checked_icon; 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    self.tableData = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T"]; 

} 

- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.tableData count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell-%d",indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)]; 
     checked_icon.tag = 1234; 
     [cell.contentView addSubview:checked_icon]; 
    } 

    cell.textLabel.text = self.tableData[indexPath.row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow]; 
    if (currentSelectedIndexPath != nil) { 

     [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor: [UIColor whiteColor]]; 
     UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:currentSelectedIndexPath]; 

     checked_icon = [selectedcell.contentView viewWithTag:1234]; 

     if (selectedcell.isSelected == YES) { 
      checked_icon.backgroundColor = [UIColor redColor]; 
     } 
     else { 
      checked_icon.backgroundColor = [UIColor clearColor]; 
     } 
    } 

    checked_icon.backgroundColor = [UIColor clearColor]; 

    return indexPath; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor lightGrayColor]]; 
    UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:indexPath]; 

    checked_icon = [selectedcell.contentView viewWithTag:1234]; 

    if (selectedcell.isSelected == YES) { 
     NSLog(@"redColor"); 
     checked_icon.backgroundColor = [UIColor clearColor]; 
    } 
    else { 
     NSLog(@"clearColor"); 
     checked_icon.backgroundColor = [UIColor clearColor]; 
    } 

    checked_icon.backgroundColor = [UIColor redColor]; 

} 


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    checked_icon = [cell.contentView viewWithTag:1234]; 

    if (cell.isSelected == YES) { 
     [cell setBackgroundColor:[UIColor lightGrayColor]]; 
     checked_icon.backgroundColor = [UIColor redColor]; 

    } 

    else { 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
     checked_icon.backgroundColor = [UIColor clearColor]; 
    } 
} 

@end 
+0

不,它不是唯一的解决方案。我个人认为这很糟糕。相反,你应该使用适当的自定义单元格。 – vikingosegundo

+0

特别是因为你正在使细胞再利用不可能。 – vikingosegundo

+0

做得更好:-)我的代码并不完美。我知道,亲爱的... –

相关问题