Ios 将子视图添加到UITableViewCell,以替换单元格选择

Ios 将子视图添加到UITableViewCell,以替换单元格选择,ios,objective-c,uitableview,addsubview,Ios,Objective C,Uitableview,Addsubview,在我的表格视图中,每个单元格通过didselectRowAtIndexPath获得一个子视图,以高亮显示当前选定的行。一切正常,但在滚动tableview的那一刻,子视图不会隐藏在以前选定的单元格中 简而言之:您将如何替换“管理单元格选择和高亮显示” 提前谢谢 这是我的密码: #import "ViewController.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate> @p

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

简而言之:您将如何替换“管理单元格选择和高亮显示”

提前谢谢

这是我的密码:

#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
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)NSArray*tableData;
@结束
@实现视图控制器
@合成选中的图标;
-(无效)viewDidLoad{
[超级视图下载];
self.tableData=@[“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”,“I”,“J”,“K”,“L”,“M”,“N”,“O”,“P”,“Q”,“R”,“S”,“T”);
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[self.tableData count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
选中的图标=[[UIView alloc]initWithFrame:cRectMake(0,0,10,44)];
选中图标。背景颜色=[UIColor redColor];
}
cell.textlab.text=self.tableData[indexPath.row];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
返回单元;
}
-(NSIndexPath*)tableView:(UITableView*)tableView将选择RowatineXpath:(NSIndexPath*)indexPath{
NSIndexPath*currentSelectedIndexPath=[tableView indexPathForSelectedRow];
如果(currentSelectedIndexPath!=nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath]setBackgroundColor:[UIColor whiteColor]];
UITableViewCell*cell=[tableView cellForRowAtIndexPath:currentSelectedIndexPath];
如果(cell.isSelected==是){
选中图标。背景颜色=[UIColor redColor];
}
否则{
选中图标。背景颜色=[UIColor clearColor];
}
}
返回索引XPath;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
[[tableView cellForRowAtIndexPath:indexPath]setBackgroundColor:[UIColor lightGrayColor]];
UITableViewCell*selectedcell=[tableView cellForRowAtIndexPath:indexPath];
[selectedcell.contentView添加子视图:选中图标];
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(NSIndexPath*)indexPath{
如果(cell.isSelected==是){
[cell setBackgroundColor:[UIColor lightGrayColor]];
}
否则{
[cell setBackgroundColor:[UIColor whiteColor]];
}
}
@结束

您应该使用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;
}

多亏尼克贝克先生

viewWithTag似乎是唯一的解决方案

下面是代码:(不完美,但有效)

#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,强)NSArray*tableData;
@结束
@实现视图控制器
@合成选中的图标;
-(无效)viewDidLoad{
[超级视图下载];
self.tableData=@[“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”,“I”,“J”,“K”,“L”,“M”,“N”,“O”,“P”,“Q”,“R”,“S”,“T”);
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[self.tableData count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString*CellIdentifier=[NSString stringWithFormat:@“单元格-%d”,indexath.row];
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
选中的图标=[[UIView alloc]initWithFrame:cRectMake(0,0,10,44)];
选中的_icon.tag=1234;
[cell.contentView addSubview:选中图标];
}
cell.textlab.text=self.tableData[indexPath.row];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
返回单元;
}
-(NSIndexPath*)tableView:(UITableView*)tableView将选择RowatineXpath:(NSIndexPath*)indexPath{
NSIndexPath*currentSelectedIndexPath=[tableView indexPathForSelectedRow];
如果(currentSelectedIndexPath!=nil){
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath]setBackgroundColor:[UIColor whiteColor]];
UITableViewCell*selectedcell=[tableView CellforRowatineIndexPath:currentSelectedIndexPath];
选中的图标=[selectedcell.contentView视图带标记:1234];
如果(selectedcell.isSelected==是){
选中图标。背景颜色=[UIColor redColor];
}
否则{
选中图标。背景颜色=[UIColor clearColor];
}
}
选中图标。背景颜色=[UIColor clearColor];
返回索引XPath;
}
-(无效)表视图:(UITableView*)表视图d
#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