【问题标题】:I have two views on one cell, when I click on a cell it will be hidden and one edit form will be expanded on that. How to resolve that?我在一个单元格上有两个视图,当我单击一个单元格时,它将被隐藏,一个编辑表单将在该单元格上展开。如何解决?
【发布时间】:2015-07-01 02:58:26
【问题描述】:

我是 iOS 初学者。我有一个带有两个自定义视图的 tableview 单元格:一个是项目单元格,一个是编辑表单该单元格。当用户单击项目时,该项目将被隐藏,并且该单元格的编辑表单将在其上展开。

我该如何解决这个问题。谢谢!

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MinimumCell *cell1 = (MinimumCell *)[tableView dequeueReusableCellWithIdentifier:@"MinimumCell"];
    if (cell1 == nil) {
        cell1 = (MinimumCell *)[MinimumCell cellFromNibNamed:@"MinimumCell"];

    }
    ExtendCell *cell2 = (ExtendCell *)[tableView dequeueReusableCellWithIdentifier:@"ExtendCell"];
    if (cell2 == nil) {
        cell2 = (ExtendCell *)[ExtendCell cellFromNibNamed:@"ExtendCell"];

    }
    if(isEditClicked && selectedIndexPath.section == indexPath.section && selectedIndexPath.row == indexPath.row ){
        return cell2;
    }
    return cell1;
}

默认显示 cell1,当用户点击该单元格时,cell1 将替换为 cell2(编辑表单)。它与展开动画一起出现。

【问题讨论】:

  • 你有代码吗
  • 我刚刚添加了我的代码
  • 点击单元格后。你只需要调用 reloadData 方法。
  • @LeVanMan 当用户点击 cell2 时会发生什么?
  • @V.J:谢谢。那是最好的吗?另外,如何在 cell2 展开时为它添加动画

标签: ios objective-c uitableview custom-cell


【解决方案1】:

在单击行时使用 didSelectRowAtIndexPath 重新加载该单元格 [tableView reloadRowsAtIndexPaths: withRowAnimation: ];

通过以下方法使用您的 cellForRowAtIndex..

如下图所示,您可以使用此方法在两个 Cell 视图之间切换。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// isEditCell is bool variable used to toggle between cell types.

isEditCell=[self shouldToggleView:[tableView cellForRowAtIndexPath:indexPath] ];

NSArray *ll=[[NSArray alloc]initWithObjects:indexPath, nil];
[tableView reloadRowsAtIndexPaths:ll withRowAnimation:UITableViewRowAnimationAutomatic];

}


-(BOOL )shouldToggleView:(UIView *)view {

while (view != nil) {
    if ([view isKindOfClass:[TableViewEditCell class]]) {
        return  NO; //[tbl indexPathForCell:(TableViewEditCell *)view];
    }else  if ([view isKindOfClass:[TableViewCell class]]) {
        return   YES;//[tbl indexPathForCell:(TableViewCell *)view];
    }
    else {
        view = [view superview];
    }
}

return nil;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多