我需要在ios 8中实现可扩展的tableView单元

编程入门 行业动态 更新时间:2024-10-26 22:22:20
本文介绍了我需要在ios 8中实现可扩展的tableView单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目中,我需要实现UITableview,其中一些tableView单元是可扩展的,其中一些是独立的。如果是可扩展细胞,则需要在此处指示'+'符号。中的图像描述。任何人都可以帮帮我

In my project I need to implement the UITableview with some of the tableView cells are expandable and some of them are independent. If it is expandable cell need to indicate the '+' symbol.enter image description here. Can any one please help me out

推荐答案

我创建了一个小型演示,

I have created a small demo,

github/haripalwagh/ExpandableTableviewCell

屏幕截图1:扩展单元格之前

Screenshot 1 : Before expanding a cell

屏幕截图2:扩展后cell

Screenshot 2 : After expanding a cell

@interface ViewController () <UITableViewDataSource, UITableViewDelegate> { UITableView *tblView; NSArray *cell0SubMenuItemsArray; BOOL isSection0Cell0Expanded; } @end @implementation ViewController # pragma mark - View Life Cycle - (void)viewDidLoad { [super viewDidLoad]; tblView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; tblView.backgroundColor = [UIColor clearColor]; tblView.delegate = self; tblView.dataSource = self; tblView.allowsSelection = YES; tblView.scrollEnabled = YES; tblView.alwaysBounceVertical = YES; [self.view addSubview:tblView]; cell0SubMenuItemsArray = @[@"First Static Menu Item", @"Second Static Menu Item", @"Third Static Menu Item"]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.view setNeedsLayout]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self updateViewDimensions]; } - (void)updateViewDimensions { tblView.frame = CGRectMake(0, 40, 320, 550); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } # pragma mark - UITableView Delegate and Datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { int cellCount = 2; // Default count - if not a single cell is expanded if (isSection0Cell0Expanded) { cellCount += [cell0SubMenuItemsArray count]; } return cellCount; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *strCellId = @"CellId"; UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCellId]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.section == 0) { if (indexPath.row == 0) { cell.textLabel.text = @"Expandable Cell"; UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; if (isSection0Cell0Expanded) // Set accessory view according to cell state - EXPANDED / NOT EXPANDED { accessoryImageView.image = [UIImage imageNamed:@"Minus.png"]; cell.detailTextLabel.text = @"Status : Expanded"; } else { accessoryImageView.image = [UIImage imageNamed:@"Plus.png"]; cell.detailTextLabel.text = @"Status : Not Expanded"; } cell.accessoryView = accessoryImageView; } else { if (isSection0Cell0Expanded && [cell0SubMenuItemsArray count] >= indexPath.row) // Check Expanded status and do the necessary changes { cell.textLabel.text = [NSString stringWithFormat:@"%@", [cell0SubMenuItemsArray objectAtIndex:indexPath.row - 1]]; } else { cell.textLabel.text = @"Static Cell"; } } } return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { // Change status of a cell reload table isSection0Cell0Expanded = !isSection0Cell0Expanded; [tblView reloadData]; } }

您必须为每个可扩展单元格进行这样的管理。 希望这会对你有帮助..

You have to manage like this for every expandable cell. Hope this will help you..

更多推荐

我需要在ios 8中实现可扩展的tableView单元

本文发布于:2023-11-27 08:47:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   ios   tableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!