Multi-level UITableView?

Asked by cambron elsey

A lot of table needs to be made. For example, at the beginning we have only 3 cells.
cc9f19a769b613a2faa7336a6a6a6337ab2c1fa.png


And beyond Level 3

I understand that this is done by simple insertRowsAtIndexPaths But my data is taken from different arrays. I think you need to use some state for each case. But I can not understand how. Maybe someone did something like that? Share snippets;)

Answers

sarah funke donovan
I did this.

At the entrance of the Mississ (NSArray), each element has a level. Run through the array and create a view with a shift to the left. Then it all shoved into 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];
    }
  }
}


then

 
-(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;	
}
Replies:
* with a left shift
with a shift to the right depending on the level - mojgan
So I did hang up the methods that filtered | sorted the array and did the reload of section - payam
What a horror. Than standard UITableViewCellStyleSubtitle, cell.textLabel, cell.detailTextLabel, cell.imageView, cell.indentationLevel and cell.indentationWidth do not suit? - danceluvr211
I do not remember exactly what they did not suit. Show a working example of what you say. With a dynamic block height, in my case, cells of different heights depending on the number of text. - anthony renfro
Is it possible to access OpenLDAP via MAPI? Or how can OpenLDAP pretend to be a GAL? :: Processing streaming video :: Advise a book on Rails 3 in Russian or English :: WordPress 3.0, ID of the current category :: Simple PHP class for Twitter API on OAuth?
Leave Repply for Multi-level UITableView?
Useful Links