-1

在我的表格视图中,每个单元格都通过 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
4

2 回答 2

0

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

编辑

正确的方法是创建 UITableViewCell 的子类并在其中引用您的复选标记。

虽然我原始帖子中的代码可以工作,但这不是最好的方法,如果您的单元格视图变得更复杂,它会很快变得复杂。

原来的

如果您不想使用“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;
}
于 2013-10-10T09:07:37.703 回答
-1

感谢尼克巴克先生!

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
于 2013-10-10T11:27:25.337 回答