Question

Ive got a chat system in my app, and im attempting to make dynamic cells to have dynamic height according to how much text is in the cell, pretty common thing people try to do, however i cant get to get mine working properly.

Also the messages align to the right, the sender is supposed to be on the left and the reciever should be on the right... heres what i have done with the storyboard.

created a TableView with 2 dynamic prototypes, inside a UIViewControllerhere is the viewController for that... each cell has a label, one left one right, the whole right and left thing work... heres my issue. Its only pulling to the right for all, so basically my if isnt happening and my else is overruling. Heres a SS.

So i have two issues... Text wont have multiple lines... along with wont do dynamic height, also... if someone can point me i the right dirrection for getting sender and reciever to show on different sides.

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *myWords = [[getMessage objectAtIndex:indexPath.row] componentsSeparatedByString:@":oyr4:"];
    if (myWords[1] == [MyClass str]){
        static NSString *sender = @"sender";
        UITableViewCell* cellSender = [_tableView dequeueReusableCellWithIdentifier:sender];
        messageContentTo = (UILabel *)[cellSender viewWithTag:83];
        self->messageContentTo.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
        self->messageContentTo.lineBreakMode = NSLineBreakByWordWrapping;
        [self->messageContentTo sizeToFit];
        messageContentTo.text = myWords[4];
        return cellSender;
    } else {

        static NSString *reciever = @"reciever";

        UITableViewCell* cellReciever = [_tableView dequeueReusableCellWithIdentifier:reciever];
        messageContentFrom = (UILabel *)[cellReciever viewWithTag:84];
        messageContentFrom.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
        messageContentFrom.lineBreakMode = NSLineBreakByWordWrapping;
        messageContentFrom.font = [UIFont systemFontOfSize:22];
        messageContentFrom.numberOfLines = 0;
        messageContentFrom.text = myWords[4];

        return cellReciever;
    }

}
#pragma mark - UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = [[getMessage objectAtIndex:indexPath.row]
                   sizeWithFont:[UIFont systemFontOfSize:22]
                   constrainedToSize:CGSizeMake(1000, CGFLOAT_MAX)];
    return size.height + 15;
}
Was it helpful?

Solution

The left-right problem might be due to this:

if (myWords[1] == [MyClass str])

If myWords[1] is a string, you need to use isEqualToString: not "==" to compare it.

if ([myWords[1] isEqualToString:[MyClass str]])

As far as the label height not adjusting properly, it's hard to tell what's going on without knowing how your labels are set up. I usually do it by making constraints between the label and the top and bottom of the cell in IB. That way, when you change the height of the cell, the label will follow (and of course, set numberOfLines to 0). Also, in your sizeWithFont:constrainedToSize: method, the width you pass into CGSizeMake() should be the width of the label, not 1000.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top