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
    • 292
    Comment on it

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

    -(void)setChatData:(ChatMessage *)messageObj memberDict:(NSDictionary *)memberDict isCurrentUser:(BOOL)isCurrentUser{
    
    
         NSDictionary *userDict = [memberDict objectForKey:messageObj.senderId];
        [message setText:messageObj.body];
    
        CGRect BGframeintial = backgoundView.frame;
        BGframeintial.size.width = MIN_WIDTH + 10;
        backgoundView.frame = BGframeintial;
    
        CGRect messgframeintial = message.frame;
        messgframeintial.size.width = MIN_WIDTH;
        message.frame = messgframeintial;
    
    //    message.numberOfLines = 1;
    
        [message sizeToFit];
        [self checkForMaxLimit];
        [message sizeToFit];
    
        [self setFrameForCell:isCurrentUser];
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    //    NSDate *date = [formatter dateFromString:[dict valueForKey:@"time"]];
    
        NSDate *date = messageObj.timeObject;
        if (date == nil) {
            [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
            date = [formatter dateFromString:messageObj.timeStamp];
        }
    
        [formatter setDateFormat:@"MMM,dd hh:mm a"];
        NSString *chatTime = [formatter stringFromDate:date];
        [time setText:chatTime];
        [time setFont:[UIFont systemFontOfSize:8]];
    
        [userName setText:[userDict objectForKey:@"name"]];
    
        [userImage setImageFromUrl:[userDict objectForKey:@"url"] borderColor:[UIColor whiteColor]];
    
        [self setBackgroundColor:[UIColor clearColor]];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    -(void)setFrameForCell:(BOOL)iscurrentUser{
        CGRect frame = backgoundView.frame;
        frame.size.height = message.frame.size.height + 18;
        frame.size.width = message.frame.size.width + 10;
    
    
        if (iscurrentUser) {
            frame.origin.x = (CURENT_USER_LIMITED - frame.size.width);
    
            CGRect messgframeintial = message.frame;
            messgframeintial.origin.x = frame.origin.x + 5;
            message.frame = messgframeintial;
    
        }
        backgoundView.frame = frame;
    
        frame = time.frame;
        frame.origin.y = backgoundView.frame.origin.y + backgoundView.frame.size.height+10 - 20;
        time.frame = frame;
    
        backgoundView.layer.cornerRadius = 5;
    }
    
    -(void)checkForMaxLimit{
        CGSize labelSize = [message.text sizeWithFont:message.font
                                    constrainedToSize:message.frame.size
                                        lineBreakMode:NSLineBreakByWordWrapping];
        int numberOfLine  =  ceilf(labelSize.height);
    
        if (numberOfLine/17 > 4) {
            CGRect BGframeintial = backgoundView.frame;
            BGframeintial.size.width = MAX_WIDTH + 10;
            backgoundView.frame = BGframeintial;
            CGRect messgframeintial = message.frame;
            messgframeintial.size.width = MAX_WIDTH;
            message.frame = messgframeintial;
    
        }
    
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: