Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create dynamic UITableViewCell for Chat

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 325
    Comment on it

    Following code will help you to create dynamic UITableViewCell w.r.t to its content of message.

    1. -(void)setChatData:(ChatMessage *)messageObj memberDict:(NSDictionary *)memberDict isCurrentUser:(BOOL)isCurrentUser{
    2.  
    3.  
    4. NSDictionary *userDict = [memberDict objectForKey:messageObj.senderId];
    5. [message setText:messageObj.body];
    6.  
    7. CGRect BGframeintial = backgoundView.frame;
    8. BGframeintial.size.width = MIN_WIDTH + 10;
    9. backgoundView.frame = BGframeintial;
    10.  
    11. CGRect messgframeintial = message.frame;
    12. messgframeintial.size.width = MIN_WIDTH;
    13. message.frame = messgframeintial;
    14.  
    15. // message.numberOfLines = 1;
    16.  
    17. [message sizeToFit];
    18. [self checkForMaxLimit];
    19. [message sizeToFit];
    20.  
    21. [self setFrameForCell:isCurrentUser];
    22.  
    23. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    24. // [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    25. // NSDate *date = [formatter dateFromString:[dict valueForKey:@"time"]];
    26.  
    27. NSDate *date = messageObj.timeObject;
    28. if (date == nil) {
    29. [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    30. date = [formatter dateFromString:messageObj.timeStamp];
    31. }
    32.  
    33. [formatter setDateFormat:@"MMM,dd hh:mm a"];
    34. NSString *chatTime = [formatter stringFromDate:date];
    35. [time setText:chatTime];
    36. [time setFont:[UIFont systemFontOfSize:8]];
    37.  
    38. [userName setText:[userDict objectForKey:@"name"]];
    39.  
    40. [userImage setImageFromUrl:[userDict objectForKey:@"url"] borderColor:[UIColor whiteColor]];
    41.  
    42. [self setBackgroundColor:[UIColor clearColor]];
    43. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    44. }
    45.  
    46. -(void)setFrameForCell:(BOOL)iscurrentUser{
    47. CGRect frame = backgoundView.frame;
    48. frame.size.height = message.frame.size.height + 18;
    49. frame.size.width = message.frame.size.width + 10;
    50.  
    51.  
    52. if (iscurrentUser) {
    53. frame.origin.x = (CURENT_USER_LIMITED - frame.size.width);
    54.  
    55. CGRect messgframeintial = message.frame;
    56. messgframeintial.origin.x = frame.origin.x + 5;
    57. message.frame = messgframeintial;
    58.  
    59. }
    60. backgoundView.frame = frame;
    61.  
    62. frame = time.frame;
    63. frame.origin.y = backgoundView.frame.origin.y + backgoundView.frame.size.height+10 - 20;
    64. time.frame = frame;
    65.  
    66. backgoundView.layer.cornerRadius = 5;
    67. }
    68.  
    69. -(void)checkForMaxLimit{
    70. CGSize labelSize = [message.text sizeWithFont:message.font
    71. constrainedToSize:message.frame.size
    72. lineBreakMode:NSLineBreakByWordWrapping];
    73. int numberOfLine = ceilf(labelSize.height);
    74.  
    75. if (numberOfLine/17 > 4) {
    76. CGRect BGframeintial = backgoundView.frame;
    77. BGframeintial.size.width = MAX_WIDTH + 10;
    78. backgoundView.frame = BGframeintial;
    79. CGRect messgframeintial = message.frame;
    80. messgframeintial.size.width = MAX_WIDTH;
    81. message.frame = messgframeintial;
    82.  
    83. }
    84.  
    85.  
    86. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: