TableView 复选标记 [英] TableView CheckMarks

查看:55
本文介绍了TableView 复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择字典数据数组时在 tableview 中放置复选标记.

例如:- 数组包含 10 个模型名称(它是字典),它包含子模型

我的问题是,当我选择 Submodel 时,ModelName 会自动获取 CheckMark.现在我为不同的模型打勾子模型,但我们如何根据子模型设置复选标记.

我的cellForRow方法

UITableViewCell *cell;cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];如果(!单元格){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle ReuseIdentifier:@"Cell"];}UILabel *nameLbl = (UILabel*) [cell.contentView viewWithTag:11];UILabel *code = (UILabel*) [cell.contentView viewWithTag:12];UIButton *button = (UIButton*) [cell.contentView viewWithTag:13];NSInteger index = indexPath.row;NSDictionary *dictParent = [_data objectAtIndex:indexPath.section];NSDictionary *dictItem = dictParent;如果(indexPath.row > 0){//如果它不是该部分的第一行,则假定该行是子行.NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];//获取子行信息dictItem = [arrChildren objectAtIndex:indexPath.row ];}nameLbl.text = [dictItem objectForKey:@"Name"];code.text = [dictItem objectForKey:@"Code"];//显示选中值的复选标记如果(_selectedarray.count == _rowdata.count){imagebutton.hidden=NO;[headerArray removeAllObjects];[headerArray addObject:@"1"];UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];[button setImage:btnImage forState:UIControlStateNormal];[按钮 setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];}else if ([_selectedarray containsObject:[_rowdata objectAtIndex:index]] ){imagebutton.hidden =NO;[headerArray removeAllObjects];[headerArray addObject:@"1"];UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];[button setImage:btnImage forState:UIControlStateNormal];[按钮 setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];}别的{imagebutton.hidden=YES;cell.accessoryType=UITableViewCellAccessoryNone;UIImage *btnImage = [UIImage imageNamed:@""];[button setImage:btnImage forState:UIControlStateNormal];[按钮 setBackgroundColor:[UIColor whiteColor]];}

根据上面的代码,我可以为多选设置复选标记.请为我的问题提供一些想法(或)示例

<代码> ({ChildProductModels = ({代码 = "LB3/7-002";Name = "带运输围裙 4.5 M";ParentChildType = C;ParentID = PMD000001;产品ID = PRD000004;产品型号 ID = PMD000003;},{代码 = "LB3/7-003";Name = "带磁辊";ParentChildType = C;ParentID = PMD000001;产品ID = PRD000004;ProductModelID = PMD000004;});代码 = "LB3/7";Name = "混合开棉机 LB3/7";ParentChildType = P;ParentID = "";产品ID = PRD000004;产品型号 ID = PMD000001;},{ChildProductModels = ({代码 = "LB7/4-001";Name = "带打手";ParentChildType = C;ParentID = PMD000005;产品ID = PRD000004;产品型号 ID = PMD000006;});代码 = "LB7/4";Name = "UNIMIX 型号 LB7/4";ParentChildType = P;ParentID = "";产品ID = PRD000004;产品型号 ID = PMD000005;})

上面是我的字典数组

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section{Header *headerView = [tableView dequeueReusableCellWithIdentifier:@"HeaderView"];UILabel *name = (UILabel*) [headerView.contentView viewWithTag:2];UILabel *code = (UILabel*) [headerView.contentView viewWithTag:4];name.text = [_data[section] valueForKey:@"Name"] ;code.text=[_data[section] valueForKey:@"Code"] ;imagebutton=(UIButton*)[headerView.contentView viewWithTag:3];UIImage *btnImage = [UIImage imageNamed:@""];[imagebutton setImage:btnImage forState:UIControlStateNormal];[图像按钮 setBackgroundColor:[UIColor whiteColor]];if(headerArray.count>0){if([headerArray containsObject:@"0"]){UIImage *btnImage = [UIImage imageNamed:@""];[imagebutton setImage:btnImage forState:UIControlStateNormal];[图像按钮 setBackgroundColor:[UIColor whiteColor]];}别的{UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];[imagebutton setImage:btnImage forState:UIControlStateNormal];[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];}}UIButton *btn=(UIButton*)[headerView.contentView viewWithTag:1];[btn addTarget: self动作:@selector(buttonClicked:)forControlEvents: UIControlEventTouchUpInside];返回标题视图;}-(void)buttonClicked:(id)sender{if(imagebutton.currentImage == [UIImage imageNamed:@""] ){UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];[imagebutton setImage:btnImage forState:UIControlStateNormal];[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];}别的{UIImage *btnImage = [UIImage imageNamed:@""];[imagebutton setImage:btnImage forState:UIControlStateNormal];[图像按钮 setBackgroundColor:[UIColor whiteColor]];}}

<块引用>

在上面我的 viewForHeader 方法

我的 TableviewdidSelect 方法

selectedIndex = indexPath.row;NSNumber *num=[NSNumber numberWithInteger:indexPath.section];如果(!_selectedarray){imagebutton.hidden=YES;[headerArray addObject:@"0"];_selectedarray = [[NSMutableArray alloc] init];}if(![_selectedarray containsObject:[_rowdata objectAtIndex:selectedIndex]]){imagebutton.hidden=NO;[headerArray removeAllObjects];[headerArray addObject:@"1"];[_selectedarray addObject:[_rowdata objectAtIndex:selectedIndex]];[dataArray addObject:[_rowdata objectAtIndex:selectedIndex]];[selectedSection addObject:num];}别的{imagebutton.hidden=YES;[headerArray addObject:@"0"];[_selectedarray removeObject:[_rowdata objectAtIndex:selectedIndex]];[dataArray removeObject:[_rowdata objectAtIndex:selectedIndex]];}[tableView reloadData];

解决方案

我利用标签"属性轻松访问 TableView 中的部分标题.

像这样:

- (void)loadData {我的数据 = @[@{@"代码":@"LB3/7",@"名称":@"混合开包机 LB3/7",@"ParentChildType":@"P",@"ParentID":[NSNull null],@"ProductID":@"PRD000004",@"ProductModelID":@"PMD000001",@"ChildProductModels":@[@{@"代码":@"LB3/7-002",@"姓名":@"带运输围裙 4.5 M",@"ParentChildType":@"C",@"ParentID":@"PMD000001",@"ProductID":@"PRD000004",@"ProductModelID":@"PMD000003"},@{@"代码":@"LB3/7-003",@"名称":@"带磁辊",@"ParentChildType":@"C",@"ParentID":@"PMD000001",@"ProductID":@"PRD000004",@"ProductModelID":@"PMD000004"}]},@{@"代码":@"LB7/4",@"名称":@"UNIMIX 型号 LB7/4",@"ParentChildType":@"P",@"ParentID":[NSNull null],@"ProductID":@"PRD000004",@"ProductModelID":@"PMD000005",@"ChildProductModels":@[@{@"代码":@"LB7/4-001",@"姓名":@"与打手",@"ParentChildType":@"C",@"ParentID":@"PMD000005",@"ProductID":@"PRD000004",@"ProductModelID":@"PMD000006"}]}];}- (void)viewDidLoad {[超级viewDidLoad];[自加载数据];arrSelectedRows = [NSMutableArray 新];myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];myTableView.dataSource = self;myTableView.delegate = self;[self.view addSubview: myTableView];}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {NSDictionary *dictParent = [myData objectAtIndex:section];//创建部分标题(替换为自定义 UIView)UILabel *lblSectionHeader = [UILabel new];lblSectionHeader.tag = 部分 + 100;//设置标签,以便我们稍后访问lblSectionHeader.text = [dictParent objectForKey:@"Name"];返回 lblSectionHeader;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {返回 myData.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {NSDictionary *dictParent = [myData objectAtIndex:section];NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];返回 arrChildren.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];如果(!单元格){单元格 = [[UITableViewCell 分配] initWithStyle:UITableViewCellStyleValue1 重用标识符:@"cellId"];[cell setSelectionStyle:UITableViewCellSelectionStyleNone];}//获取父项NSDictionary *dictParent = [myData objectAtIndex:indexPath.section];//获取孩子NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];//获取子行信息NSDictionary *dictItem = [arrChildren objectAtIndex:indexPath.row];cell.textLabel.text = [dictItem objectForKey:@"Name"];cell.detailTextLabel.text = [dictItem objectForKey:@"Code"];//确保在填充行时设置了附件类型if ([arrSelectedRows containsObject:indexPath]) {[cell setAccessoryType:UITableViewCellAccessoryCheckmark];} 别的 {[cell setAccessoryType:UITableViewCellAccessoryNone];}返回单元格;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];if ([arrSelectedRows containsObject:indexPath]) {//如果选中的行已经被选中,取消选中它[arrSelectedRows removeObject:indexPath];[tableView deselectRowAtIndexPath:indexPath 动画:YES];[cell setAccessoryType:UITableViewCellAccessoryNone];//检查是否所有子项都被取消选择NSInteger numRowsInSection = [tableView numberOfRowsInSection:indexPath.section];BOOL areChildrenDeselected = true;for (NSInteger i = 0; i 

......在下面的某些内容中导致父/段标题在选择子项时自动设置为蓝色.

I Want to Put CheckMarks in tableview when selecting array of dictionaries data.

Ex:- Array contains 10 Model Names(It is Dictionary), It contains SubModels

My problem is,When I select Submodel, ModelName automatically get CheckMark. Now I Put CheckMarks for different models & sub Models but how we can put checkmarks based on SubModels.

My cellForRow method

UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
UILabel *nameLbl = (UILabel*) [cell.contentView viewWithTag:11];
UILabel *code = (UILabel*) [cell.contentView viewWithTag:12];
UIButton *button = (UIButton*) [cell.contentView viewWithTag:13];
NSInteger index = indexPath.row;
NSDictionary *dictParent = [_data objectAtIndex:indexPath.section];
NSDictionary *dictItem = dictParent;
if (indexPath.row > 0)
{
    // If its not the first row in the section, assume the row is a child row.
    NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];
    // Get child row info
    dictItem = [arrChildren objectAtIndex:indexPath.row ];
}
    nameLbl.text = [dictItem objectForKey:@"Name"];
    code.text = [dictItem objectForKey:@"Code"];
// To display checkmark for selected value
    if (_selectedarray.count == _rowdata.count)
    {
        imagebutton.hidden=NO;
        [headerArray removeAllObjects];
        [headerArray addObject:@"1"];
        UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];
        [button setImage:btnImage forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
    }
    else if ([_selectedarray containsObject:[_rowdata objectAtIndex:index]] )
    {
        imagebutton.hidden =NO;
        [headerArray removeAllObjects];
        [headerArray addObject:@"1"];
        UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];
        [button setImage:btnImage forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
    }
    else
    {
        imagebutton.hidden=YES;
        cell.accessoryType=UITableViewCellAccessoryNone;
        UIImage *btnImage = [UIImage imageNamed:@""];
        [button setImage:btnImage forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
    }

From the above code I am Able to put checkmarks for multiple selection.Please give some idea (OR) example for My problem

 (
{
 ChildProductModels =     (
        {
    Code = "LB3/7-002";
    Name = "With transport apron 4.5 M";
    ParentChildType = C;
    ParentID = PMD000001;
    ProductID = PRD000004;
    ProductModelID = PMD000003;
},
        {
    Code = "LB3/7-003";
    Name = "With Magnetic Roller";
    ParentChildType = C;
    ParentID = PMD000001;
    ProductID = PRD000004;
    ProductModelID = PMD000004;
  }
);
Code = "LB3/7";
Name = "Mixing Bale Opener LB3/7";
ParentChildType = P;
ParentID = "<null>";
ProductID = PRD000004;
ProductModelID = PMD000001;
},
{
   ChildProductModels =     (
        {
    Code = "LB7/4-001";
    Name = "With Beater";
    ParentChildType = C;
    ParentID = PMD000005;
    ProductID = PRD000004;
    ProductModelID = PMD000006;
  }
  );
  Code = "LB7/4";
 Name = "UNIMIX MODEL LB7/4";
 ParentChildType = P;
 ParentID = "<null>";
  ProductID = PRD000004;
 ProductModelID = PMD000005;
}
)

Above I put my array of dictionaries

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:   (NSInteger)section
{
Header *headerView = [tableView dequeueReusableCellWithIdentifier:@"HeaderView"];
UILabel *name = (UILabel*) [headerView.contentView viewWithTag:2];
UILabel *code = (UILabel*) [headerView.contentView viewWithTag:4];
name.text = [_data[section] valueForKey:@"Name"] ;
code.text=[_data[section] valueForKey:@"Code"] ;
imagebutton=(UIButton*)[headerView.contentView viewWithTag:3];
UIImage *btnImage = [UIImage imageNamed:@""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
    if(headerArray.count>0)
    {
        if([headerArray containsObject:@"0"])
        {
            UIImage *btnImage = [UIImage imageNamed:@""];
            [imagebutton setImage:btnImage forState:UIControlStateNormal];
            [imagebutton setBackgroundColor:[UIColor whiteColor]];
        }
        else
        {
            UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];
            [imagebutton setImage:btnImage forState:UIControlStateNormal];
            [imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
        }
}
UIButton *btn=(UIButton*)[headerView.contentView viewWithTag:1];
[btn addTarget: self
        action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
return headerView;
}

-(void)buttonClicked:(id)sender
{
if(imagebutton.currentImage == [UIImage imageNamed:@""] )
{
    UIImage *btnImage = [UIImage imageNamed:@"ic_floating_done_@1x"];
    [imagebutton setImage:btnImage forState:UIControlStateNormal];
    [imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];

}
else
{
    UIImage *btnImage = [UIImage imageNamed:@""];
    [imagebutton setImage:btnImage forState:UIControlStateNormal];
    [imagebutton setBackgroundColor:[UIColor whiteColor]];
}
}

In above My viewForHeader method

My TableviewdidSelect Method

selectedIndex = indexPath.row;
NSNumber *num=[NSNumber numberWithInteger:indexPath.section];
    if (!_selectedarray)
    {
        imagebutton.hidden=YES;
        [headerArray addObject:@"0"];
        _selectedarray = [[NSMutableArray alloc] init];
    }
    if(![_selectedarray containsObject:[_rowdata objectAtIndex:selectedIndex]])
    {
        imagebutton.hidden=NO;
        [headerArray removeAllObjects];
        [headerArray addObject:@"1"];
        [_selectedarray addObject:[_rowdata objectAtIndex:selectedIndex]];
        [dataArray addObject:[_rowdata objectAtIndex:selectedIndex]];
        [selectedSection addObject:num];
    }
    else
    {
        imagebutton.hidden=YES;
        [headerArray addObject:@"0"];
        [_selectedarray removeObject:[_rowdata objectAtIndex:selectedIndex]];
        [dataArray removeObject:[_rowdata objectAtIndex:selectedIndex]];

    }
[tableView reloadData];

解决方案

Im utilizing the "tag" attribute to easily access the Section header in the TableView.

Something like this:

- (void)loadData {
    myData = @[
               @{
                   @"Code":@"LB3/7",
                   @"Name":@"Mixing Bale Opener LB3/7",
                   @"ParentChildType":@"P",
                   @"ParentID":[NSNull null],
                   @"ProductID":@"PRD000004",
                   @"ProductModelID":@"PMD000001",
                   @"ChildProductModels":@[
                           @{
                               @"Code":@"LB3/7-002",
                               @"Name":@"With transport apron 4.5 M",
                               @"ParentChildType":@"C",
                               @"ParentID":@"PMD000001",
                               @"ProductID":@"PRD000004",
                               @"ProductModelID":@"PMD000003"
                               },
                           @{
                               @"Code":@"LB3/7-003",
                               @"Name":@"With Magnetic Roller",
                               @"ParentChildType":@"C",
                               @"ParentID":@"PMD000001",
                               @"ProductID":@"PRD000004",
                               @"ProductModelID":@"PMD000004"
                               }
                           ]
                   },
               @{
                   @"Code":@"LB7/4",
                   @"Name":@"UNIMIX MODEL LB7/4",
                   @"ParentChildType":@"P",
                   @"ParentID":[NSNull null],
                   @"ProductID":@"PRD000004",
                   @"ProductModelID":@"PMD000005",
                   @"ChildProductModels":@[
                           @{
                               @"Code":@"LB7/4-001",
                               @"Name":@"With Beater",
                               @"ParentChildType":@"C",
                               @"ParentID":@"PMD000005",
                               @"ProductID":@"PRD000004",
                               @"ProductModelID":@"PMD000006"
                               }
                           ]
                   }
               ];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];
    arrSelectedRows = [NSMutableArray new];

    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    [self.view addSubview: myTableView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSDictionary *dictParent = [myData objectAtIndex:section];

    // Create section header (replace with custom UIView)
    UILabel *lblSectionHeader = [UILabel new];
    lblSectionHeader.tag = section + 100; // Set tag, so we can access it later
    lblSectionHeader.text = [dictParent objectForKey:@"Name"];
    return lblSectionHeader;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return myData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictParent = [myData objectAtIndex:section];
    NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];
    return arrChildren.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    // Get the parent item
    NSDictionary *dictParent = [myData objectAtIndex:indexPath.section];

    // Get children
    NSArray *arrChildren = [dictParent objectForKey:@"ChildProductModels"];

    // Get child row info
    NSDictionary *dictItem = [arrChildren objectAtIndex:indexPath.row];

    cell.textLabel.text = [dictItem objectForKey:@"Name"];
    cell.detailTextLabel.text = [dictItem objectForKey:@"Code"];

    // Make sure accessory type is set when the rows are populated
    if ([arrSelectedRows containsObject:indexPath]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([arrSelectedRows containsObject:indexPath]) {
        // If the selected row was already selected, deselect it
        [arrSelectedRows removeObject:indexPath];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [cell setAccessoryType:UITableViewCellAccessoryNone];

        // Check if all children are deselected
        NSInteger numRowsInSection = [tableView numberOfRowsInSection:indexPath.section];
        BOOL areChildrenDeselected = true;
        for (NSInteger i = 0; i < numRowsInSection; i++) {
            NSIndexPath *childIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            if ([arrSelectedRows containsObject:childIndexPath]) {
                areChildrenDeselected = false;
            }
        }

        // Get the section header
        UILabel *lblSectionHeader = (UILabel *)[tableView viewWithTag: 100 + indexPath.section];
        if (areChildrenDeselected) {
            lblSectionHeader.textColor = [UIColor blackColor];
        } else {
            lblSectionHeader.textColor = [UIColor blueColor];
        }

    } else {
        // If the selected row wasnt selected, select it
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        [arrSelectedRows addObject:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

        // Get section header
        UILabel *lblSectionHeader = (UILabel *)[tableView viewWithTag: 100 + indexPath.section];
        lblSectionHeader.textColor = [UIColor blueColor];
    }
}

...which results in something like this below, where the parent/section header is automatically set to blue when a child is selected.

这篇关于TableView 复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆