ios – TableView CheckMarks

ios – TableView CheckMarks,第1张

概述我想在选择字典数据数组时将CheckMark放在tableview中. 例如: – 数组包含10个模型名称(它是字典),它包含子模型 我的问题是,当我选择Submodel时,ModelName会自动获得CheckMark. 现在我把CheckMarks用于不同的型号和sub模型,但我们如何根据SubModel放置复选标记. 我的cellForRow方法 UITableViewCell *cell; 我想在选择字典数据数组时将checkmark放在tablevIEw中.

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

我的问题是,当我选择Submodel时,Modelname会自动获得checkmark.
现在我把checkmarks用于不同的型号和sub模型,但我们如何根据SubModel放置复选标记.

我的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]];    }

从上面的代码我可以为多个选择添加复选标记.请给我的问题一些想法(OR)示例

({ 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;})

上面我把我的一系列词典

- (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];
解决方法 我利用“tag”属性轻松访问tableVIEw中的Section标头.

像这样的东西:

- (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",@"ProductModelID":@"PMD000003"                               },@{                               @"Code":@"LB3/7-003",@"name":@"With Magnetic Roller",@"ProductModelID":@"PMD000004"                               }                           ]                   },@{                   @"Code":@"LB7/4",@"name":@"UNIMIX MODEL LB7/4",@"ProductModelID":@"PMD000005",@"ChildProductModels":@[                           @{                               @"Code":@"LB7/4-001",@"name":@"With Beater",@"ParentID":@"PMD000005",@"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];    }}

…导致如下所示的内容,其中父/节标题在选择子节点时自动设置为蓝色.

总结

以上是内存溢出为你收集整理的ios – TableView CheckMarks全部内容,希望文章能够帮你解决ios – TableView CheckMarks所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067113.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存