Ios TableView复选标记

Ios TableView复选标记,ios,objective-c,uitableview,nsdictionary,Ios,Objective C,Uitableview,Nsdictionary,我想在选择字典数组数据时在tableview中打勾 例如:-数组包含10个模型名(它是字典),它包含子模型 我的问题是,当我选择子模型时,ModelName会自动获得选中标记。 现在我为不同的模型和子模型打勾,但是我们如何基于子模型打勾 我的cellForRow方法 UITableViewCell *cell; cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITab

我想在选择字典数组数据时在tableview中打勾

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

我的问题是,当我选择子模型时,ModelName会自动获得选中标记。 现在我为不同的模型和子模型打勾,但是我们如何基于子模型打勾

我的
cellForRow
方法

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]];
    }
从上面的代码中,我可以为多重选择打勾。请为我的问题给出一些想法(或)例子

 (
{
 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;
}
)
在上面我的viewForHeader方法中

我的TableviewdidSelect方法


要放置复选标记,可以在复选标记的适当位置放置UIImageView

然后,您可以维护一个选定单元格的数组,其中将包含一个布尔值,isSelected(或任何您认为合适的值)

然后,一旦用户选择了一个单元格,在didSelect委托方法中。只需重新加载rows:atIndexPath即可重新加载单元格。

Im利用“tag”属性轻松访问TableView中的节标题

大概是这样的:

- (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];
    }
}
-(无效)加载数据{
myData=@[
@{
@“代码”:@“LB3/7”,
@“名称”:@“混合开捆机LB3/7”,
@“ParentChildType”:@“P”,
@“父ID”:[NSNull],
@“产品ID”:@“PRD000004”,
@“ProductModelID”:@“PMD000001”,
@“ChildProductModels”:@[
@{
@“代码”:@“LB3/7-002”,
@“名称”:@“带4.5米的运输停机坪”,
@“ParentChildType”:@“C”,
@“ParentID”:@“PMD000001”,
@“产品ID”:@“PRD000004”,
@“ProductModelID”:@“PMD00003”
},
@{
@“代码”:@“LB3/7-003”,
@“名称”:@“带磁辊”,
@“ParentChildType”:@“C”,
@“ParentID”:@“PMD000001”,
@“产品ID”:@“PRD000004”,
@“ProductModelID”:@“PMD00004”
}
]
},
@{
@“代码”:@“LB7/4”,
@“名称”:@“UNIMIX LB7/4型”,
@“ParentChildType”:@“P”,
@“父ID”:[NSNull],
@“产品ID”:@“PRD000004”,
@“ProductModelID”:@“PMD000005”,
@“ChildProductModels”:@[
@{
@“代码”:@“LB7/4-001”,
@“名称”:@“带搅拌器”,
@“ParentChildType”:@“C”,
@“家长ID”:@“PMD00005”,
@“产品ID”:@“PRD000004”,
@“ProductModelID”:@“PMD000006”
}
]
}
];
}
-(无效)viewDidLoad{
[超级视图下载];
[自动加载数据];
arrSelectedRows=[NSMutableArray new];
myTableView=[[UITableView alloc]initWithFrame:self.view.bounds样式:UITableView样式分组];
myTableView.dataSource=self;
myTableView.delegate=self;
[self.view addSubview:myTableView];
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
NSDictionary*dictParent=[myData objectAtIndex:section];
//创建节标题(替换为自定义UIView)
UILabel*lblSectionHeader=[UILabel new];
lblSectionHeader.tag=section+100;//设置标记,以便稍后访问
lblSectionHeader.text=[dictParent objectForKey:@“Name”];
返回lblSectionHeader;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回myData.count;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
NSDictionary*dictParent=[myData objectAtIndex:section];
NSArray*arrChildren=[dictParent objectForKey:@“ChildProductModels”];
返回arr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cellId]”;
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:@“cellId”];
[单元格设置选择样式:UITableViewCellSelectionStyleNone];
}
//获取父项
NSDictionary*dictParent=[myData objectAtIndex:indexath.section];
//生孩子
NSArray*arrChildren=[dictParent objectForKey:@“ChildProductModels”];
//获取子行信息
NSDictionary*dictItem=[arrChildren-objectAtIndex:indexath.row];
cell.textLabel.text=[dictItem objectForKey:@“Name”];
cell.detailTextLabel.text=[dictItem objectForKey:@“代码”];
//确保在填充行时设置了附件类型
if([arrSelectedRows containsObject:indexPath]){
[单元格设置访问类型:UITableViewCellAccessoryCheckmark];
}否则{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView cellForRowAtIndexPath:indexPath];
if([arrSelectedRows containsObject:indexPath]){
//如果已选择所选行,请取消选择该行
[arrSelectedRows-removeObject:indexPath];
[tableView取消行索引路径:indexPath动画:是];
[cell setAccessoryType:UITableViewCellAccessoryNone];
//检查是否取消选择了所有子项
NSInteger numRowsInSection=[tableView numberOfRowsInSection:indexPath.section];
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];
- (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];
    }
}