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?

不羁岁月 提交于 2019-12-24 17:30:23

问题


I'm a beginner iOS. I have one cell of tableview with two custom views: one is item cell and one is edit form that cell. When user click on item, that item will be hidden and the edit form of that cell will be expanded on that.

How I can resolve that problem. Thank you!

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

The default it will show cell1, when user click on that cell, cell1 is replace with cell2 (edit form). It is appear with expand animation.


回答1:


Use didSelectRowAtIndexPath on click of row to reload that cell using [tableView reloadRowsAtIndexPaths: withRowAnimation: ];

Use your cellForRowAtIndex with following methods..

As shown below ,you can toggle between two Cell view using this method.

-(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;

}


来源:https://stackoverflow.com/questions/31151853/i-have-two-views-on-one-cell-when-i-click-on-a-cell-it-will-be-hidden-and-one-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!