表视图复选标记
Posted
技术标签:
【中文标题】表视图复选标记【英文标题】:TableView CheckMarks 【发布时间】:2018-02-23 12:53:39 【问题描述】:我想在选择字典数据数组时在 tableview 中放置复选标记。
例如:- 数组包含 10 个模型名称(它是字典),它包含子模型
我的问题是,当我选择 Submodel 时,ModelName 会自动获取 CheckMark。 现在我为不同的模型和子模型添加了复选标记,但是我们如何根据子模型添加复选标记。
我的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;
)
上面是我的字典数组
- (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]];
在上面我的 viewForHeader 方法
我的 TableviewdidSelect 方法
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];
【问题讨论】:
真的很难理解你的意思。这是正确的:您想让用户从模型和子模型列表中选择表格视图中的行吗?问题是如何显示模型/子模型列表还是选择子模型? 您是使用默认的 UITableViewCell 还是您的单元格上有自定义 UI?您是否尝试过调试视图层次结构? 是的,我正在使用默认的 UItableviewCell @Emmanuel Ay 我在表格视图中显示模型和子模型,如果用户选择子模型,那么模型也会得到复选标记 @narasimha 我很乐意帮助你,但要做到这一点,我需要看看你的模型/子模型是什么样的。你能给我看一个模型的例子——数组和字典结构——你有吗? 【参考方案1】:我正在利用“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",
@"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];
......在下面的某些内容中导致父/段标题在选择子项时会自动设置为蓝色。
【讨论】:
但在上面的例子中,您也在行我希望在第一个显示父节点中添加父级标题.I知道部分标题未包含复选标记,因此当行选定的部分标题时,请按按钮也选择 span> 让我看看我的理解是否正确:应该有一个节标题显示父级 - 节标题应该有一个按钮显示是否选择了任何子级?你能提供一张图片或任何描述你的意思的东西吗? 是的,在没有选择行之前,这里的节标题不会取消选择 我明白了。今天晚些时候我会给你一个完整的例子。 @narasimha 查看我的示例。我为 SectionHeaderView 使用 UILabel,但您可以轻松地将其更改为看起来更好的自定义内容。【参考方案2】:要放置复选标记,您可以在适当的位置放置一个 UIImageView 来放置复选标记。
然后你可以维护一个选定单元格的数组,其中将包含一个布尔值 isSelected(或任何你认为好的)。
然后,一旦用户选择了一个单元格,在 didSelect 委托方法中。只需通过 reloadRows:atIndexPath 重新加载您的单元格。
【讨论】:
感谢@iosDeveloper 我正在使用按钮但我不明白为什么无法选择第一部分以上是关于表视图复选标记的主要内容,如果未能解决你的问题,请参考以下文章