问题
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