Multi-Nível De UITableView?

Precisa fazer um monte de уровненую tabela. Por exemplo, no início temos apenas 3 células
cc9f19a769b613a2fcaa7337ab02c1fa.png


Тапая de primeira temos adicionados célula de segundo nível 1d09805ad46be36022dc59a7bb3b88f1.png


E sobre 3º nível


Eu entendo que isso é feito simples insertRowsAtIndexPaths Mas tenho informações são obtidas a partir de diferentes matrizes. Então eu acho que precisa usar o estado para cada caso. Mas não consigo entender como. Pode alguém fez algo semelhante? Compartilhe сниппетами ;)


Answers 1:

Share:


Deena MacGregor
Answer 1

Fez similar.

Na entrada миссив (NSArray), cada item possui um nível. Пробегал a matriz e criou вьювы deslocado para a esquerda. Então é tudo пихал em UITableViewCell.

-(void) flateCommentsData:(NSArray *)_data withLevel:(NSInteger)level
{
  NSEnumerator * dataEnum = [_data objectEnumerator];
  
  NSDictionary * commentData;
  
  while (commentData = [dataEnum nextObject])
  {
    SModelComment * commentModel = [[SModelComment alloc]init];
    
    [commentModel set:@"id" withValue:[commentData objectForKey:@"id"]];
    [commentModel set:@"text" withValue:[commentData objectForKey:@"text"]];
    [commentModel set:@"userId" withValue:[commentData objectForKey:@"userId"]];
    [commentModel setLevel:level];
    
    CGRect cellRect = CGRectMake(commentModel.level*5, 0, 320 - commentModel.level*5, 0);
    UIView * cellView = [[UIView alloc] initWithFrame: cellRect];
    
    //add avatar
    SModelUser * user = [SModelUserPeer loadObjectById:[commentModel get:@"userId"]];
    UIImageView * avatarView = [[UIImageView alloc] initWithImage: user.smallAvatar.image ];
    avatarView.frame = CGRectMake(5, 7, 35, 35);
    [cellView addSubview:avatarView];
    
    //add username
    UILabel * usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, cellRect.size.width - 75, 20)];
    usernameLabel.font = [UIFont boldSystemFontOfSize:14.0];
    usernameLabel.text = [user get:@"fullName"];
    [cellView addSubview:usernameLabel];
        
    //add text
    CGRect cellTextRect = CGRectMake(45, 25, cellRect.size.width - 75, 0);
    UILabel * cellTextLabel = [[UILabel alloc] initWithFrame:cellTextRect];
    cellTextLabel.text = [commentModel get:@"text"];
    cellTextLabel.font = [UIFont systemFontOfSize:12.0];
    cellTextLabel.numberOfLines = 0;
    [cellTextLabel sizeToFit];
    cellTextLabel.backgroundColor = [UIColor whiteColor];
    [cellView addSubview:cellTextLabel];
    
    
    CGRect textFrame = cellTextLabel.frame;
    textFrame.size.height = textFrame.size.height + 10 + usernameLabel.frame.size.height
    ;
    textFrame.size.height = textFrame.size.height > 50 ? textFrame.size.height : 50;

    cellRect.size.height = textFrame.size.height;
    cellView.frame = cellRect;
        
    NSMutableDictionary * item = [[NSMutableDictionary alloc] initWithCapacity:2];
    [item setObject:cellView forKey:@"view"];
    [item setObject:commentModel forKey:@"model"];
    
    [commentsList addObject:item];
    
    if ([commentData objectForKey:@"childs"])
    {
      [self flateCommentsData:[commentData objectForKey:@"childs"] withLevel:level+1];
    }
  }
}


então

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSDictionary * item = [commentsList objectAtIndex:indexPath.row]; 
	UIView * cellView   = [item objectForKey:@"view"];
	SModelComment * commentModel = [item objectForKey:@"model"];
	
	NSString * CellIdentifier = [NSString stringWithFormat:@"comment_cell_%d", [commentModel get:@"id"]];
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	[cell.contentView addSubview:cellView];

    return cell;	
}