iOS*_*per 20 customization objective-c uitableview ios
我有一个自定义单元格的表格视图和每个单元格中的一些按钮.单击单元格内的任何按钮将显示该单元格下方的另一个自定义视图.下一步单击同一按钮将折叠视图,并且所有单元格都需要相同我尝试使用insertrow方法单击按钮但是徒劳无功.如何仅使用表视图委托来完成此操作.
这是我试过的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell_For_Dashboard";
CustomCellFor_Dashboard *customCell = (CustomCellFor_Dashboard *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (customCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellFor_Dashboard" owner:self options:nil];
customCell = [nib objectAtIndex:0];
}
[customCell.howyoulfeelBtn addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
customCell.nameLabel.text = @"test";
customCell.imgView.image = [UIImage imageNamed:@"Default.png"];
// customCell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
return customCell;
}
-(void)buttonclicked:(id)sender{
NSIndexPath *indexPath = [myTable indexPathForCell:sender];
[myTable beginUpdates];
NSIndexPath *insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
[myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
eag*_*349 33
我在一个项目上完成了同样的任务,只有一个不同的东西:没有按钮,只需点击单元格就可以展开或折叠它.
您应该在代码中编辑几件事.首先,按钮方法代码如下所示:
- (void) collapseExpandButtonTap:(id) sender
{
UIButton* aButton = (UIButton*)sender; //It's actually a button
NSIndexPath* aPath = [self indexPathForCellWithButtonTag:aButton.tag]; //Let's assume that you have only one section and button tags directly correspond to rows of your cells.
//expandedCells is a mutable set declared in your interface section or private class extensiont
if ([expandedCells containsObject:aPath])
{
[expandedCells removeObject:aPath];
}
else
{
[expandedCells addObject:aPath];
}
[myTableView beginEditing];
[myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse
}
Run Code Online (Sandbox Code Playgroud)
现在第二件事是UITableViewDelegate方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([expandedCells containsObject:indexPath])
{
return kExpandedCellHeight; //It's not necessary a constant, though
}
else
{
return kNormalCellHeigh; //Again not necessary a constant
}
}
Run Code Online (Sandbox Code Playgroud)
这里的关键是确定您的单元格是否应该展开/折叠并在委托方法中返回正确的高度.
Kal*_*ade 17
关于@ eagle.dan.1349所说的,这就是在单击单元格时如何做到这一点.在storyboard中,您还需要将表格单元格设置为剪辑子视图,否则将显示隐藏的内容.
.H
@property (strong, nonatomic) NSMutableArray *expandedCells;
Run Code Online (Sandbox Code Playgroud)
.M
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.expandedCells containsObject:indexPath])
{
[self.expandedCells removeObject:indexPath];
}
else
{
[self.expandedCells addObject:indexPath];
}
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight = 150;
CGFloat kNormalCellHeigh = 50;
if ([self.expandedCells containsObject:indexPath])
{
return kExpandedCellHeight; //It's not necessary a constant, though
}
else
{
return kNormalCellHeigh; //Again not necessary a constant
}
}
Run Code Online (Sandbox Code Playgroud)
看到这篇文章,只想给我2美分,因为我的解决方案非常类似于所选择的答案(整个区域的攻击).
许多人通过单独使用单元格来构建这一点,但我相信有一种方法可以构建它,可以更好地与人们想要实现的目标保持一致:
有标题,有细胞.标题应该是可点击的,然后标题下面的单元格将显示或隐藏.这可以通过向标题添加手势识别器来实现,当点击时,您只需删除该标题下的所有单元格(该部分),反之亦然(添加单元格).当然,您必须维护哪些标题为"打开"以及哪些标题为"已关闭"的状态.
这很好,原因有两个:
我创建了一个非常简单的库来实现这一目标.只要您的表视图设置了UITableView节标题和单元格,您所要做的就是将tableview和标题子类化.
链接:https://github.com/fuzz-productions/FZAccordionTableView
| 归档时间: |
|
| 查看次数: |
50593 次 |
| 最近记录: |