Here in this image the first section is not expanded whereas second section expands on click.
扩展的单元格也应该在didselect上转到viewcontroller 所有部分的行中的数据都是静态的。 我怎样才能在目标c?中实现这一点?我试着寻找这个问题但却找不到。
答案 0 :(得分:2)
使用以下代码将可扩展单元格拖放到UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
[cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
return cell;
}
扩展代码&折叠行将进入TableView的DidSelectRow
方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar ) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
最后,一种帮助最小化 - 最大化/扩展 - 折叠行的方法:
-(void)miniMizeThisRows:(NSArray*)ar{
for(NSDictionary *dInner in ar ) {
NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:@"Objects"];
if(arInner && [arInner count]>0){
[self miniMizeThisRows:arInner];
}
if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[self.arForTable removeObjectIdenticalTo:dInner];
[self.tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
}
}
答案 1 :(得分:1)
首先我为每个静态单元格创建了属性,在viewDidLoad中我将它们设置为隐藏:
- (void)viewDidLoad
{
[super viewDidLoad];
isExpanded = NO;
self.cell2.hidden = YES;
self.cell3.hidden = YES;
self.cell4.hidden = YES;
}
然后在DidSelectRowAtIndex:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section]== 1 && [indexPath row]==0)
{
new1 =[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
new2 =[NSIndexPath indexPathForRow:indexPath.row+2 inSection:indexPath.section];
new3 =[NSIndexPath indexPathForRow:indexPath.row+3 inSection:indexPath.section];
self.cell2 = [super tableView:tableView cellForRowAtIndexPath:new1];
self.cell3 = [super tableView:tableView cellForRowAtIndexPath:new2];
self.cell4 = [super tableView:tableView cellForRowAtIndexPath:new3];
CATransition *animate = [CATransition animation];
animate.type = kCATransitionMoveIn;
animate.duration = 0.4;
[self.cell2.layer addAnimation:animate forKey:nil];
[self.cell3.layer addAnimation:animate forKey:nil];
[self.cell4.layer addAnimation:animate forKey:nil];
if (isExpanded == NO)
{
self.cell4.hidden = NO;
self.cell2.hidden = NO;
self.cell3.hidden = NO;
isExpanded = YES; }
else
{
isExpanded = NO;
self.cell4.hidden = YES;
self.cell2.hidden = YES;
self.cell3.hidden = YES;
}
}
}
我不确定它有多好......如果有任何错误,请告诉我,因为我对目标c很新。