TableView CheckMarks

情到浓时终转凉″ 提交于 2019-12-10 15:37:33

问题


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];

回答1:


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.




回答2:


To place check marks you can have an UIImageView at the appropriate place for the check mark.

And then you can maintain an array of selected cells, which will contain a boolean, isSelected(or whatever seems good to you).

Then once the user selected a cell, in didSelect delegate method. Just reload you cell by reloadRows:atIndexPath.



来源:https://stackoverflow.com/questions/48948399/tableview-checkmarks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!