我是iOS的初学者。我有一个表格视图的单元格和两个自定义视图:一个是项目单元格,一个是编辑该单元格。当用户单击项目时,该项目将被隐藏,并且该单元格的编辑窗体将在此基础上展开。
我该如何解决那个问题。非常感谢。
(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(编辑表单)。它与展开动画一起出现。
单击行时使用didSelectRowAtIndexPath以使用[tableView重载RowsAtIndexPaths:withRowAnimation:];
使用以下方法使用cellForRowAtIndex。。
如下所示,可以使用此方法在两个"单元格"视图之间切换。
-(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;
}