将子视图添加到UITableViewCell作为单元格选择的替代

时间:2013-10-10 08:42:02

标签: ios objective-c uitableview addsubview

在我的tableview中,每个单元格都通过didselectRowAtIndexPath获取子视图以突出显示当前选定的行。一切正常,但在滚动表视图的那一刻,子视图不会隐藏在之前选择的单元格中。

简而言之:您如何替代“管理细胞选择和突出显示”?

提前致谢!

这是我的代码:

#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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:-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