Quote:
Originally Posted by yecine06
How did you manage for the backgournd to fit the content? specially the width of the baloon?
|
Here is how I did it....
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ThreadSCell *cell = (ThreadSCell*)[tableView dequeueReusableCellWithIdentifier:@"MsgListCell"];
// If no cell is available, create a new one using the given identifier
if (cell == nil) {
cell = [[[ThreadSCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"MsgListCell"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSMutableDictionary *tempMsg = [myMessages objectAtIndex:indexPath.row];
CGFloat result = 20.0;
CGSize textSize = { 260.0, 20000.0 }; // width and height of text area
NSMutableDictionary *dict = [myMessages objectAtIndex:indexPath.row];
NSString *aMsg = [dict objectForKey:@"msgBody"];
CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
result = MAX(size.height + 20, 30.0);
//NSLog(@"Height for row2:%i %f", indexPath.row, result);
// Set the cell properities
[cell.msgText setText:[tempMsg objectForKey:@"msgBody"]];
[myTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if ( indexPath.row % 2 == 0 ) {
[cell.msgText setFrame:CGRectMake(20.0, 0.0, size.width, cell.frame.size.height-10)];
//[cell.msgText setBackgroundColor:[UIColor colorWithRed:.788 green:.788 blue:.788 alpha:1]];
//[cell.msgText setText:[NSString stringWithFormat:@"H1: %f H2: %f", result, cell.frame.size.height]];
UIImage* balloon = [[UIImage imageNamed:@"msgGray.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width+35, size.height+20)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
}
Basically I create a view the same size as the cell, and a view the size of the balloon. add the balloon view to the cell view and it works. Its kinda hacked together but its good enough for now.