0

I have a cell. Whenever the text in cell row is equal to "(null)" I want the label to be on the right hand side of the the cell.

Here is my code at the moment, but it isn't doing anything. No errors, it just doesn't align to the right hand side of the cell. Any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"ChatListItem";
     NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if([[itemAtIndex objectForKey:@"user"] isEqualToString:@"(null)"]){
        cell.textLabel.textAlignment=UITextAlignmentRight;
        cell.detailTextLabel.textAlignment=UITextAlignmentRight;
    }
    cell.textLabel.text = [itemAtIndex objectForKey:@"text"];
    cell.detailTextLabel.text = [itemAtIndex objectForKey:@"user"];

   return cell;
}
spogebob92
  • 1,474
  • 4
  • 23
  • 32
  • You can't align the tablviewcell label create own custom label and add it onto you cell then you can easily set alignment as you want. – Mohit Jul 19 '14 at 19:16

2 Answers2

2

First, did you step through the code and check the contents of the value for keys "user" and "text"?

If all is as expected, you should do the following:

  1. Replace UITextAlignmentRight with NSTextAlignmentRight to silence compiler warnings.
  2. Explicitly set NSTextAlignmentRight and NSTextAlignmentLeft, otherwise you will not get the correct update in recycled cells.
  3. Finally, make sure the label's width is fixed. Otherwise, the width of the label will be based on its content, so that the alignment (within the label) loses its effect.
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I can't seem to adjust the label size, really simple thing, but there appears no way to do it? – spogebob92 Jul 19 '14 at 15:42
  • Set the `frame` of the label in `tableView:willDisplayCellForIndexPath:`. – Mundi Jul 19 '14 at 15:54
  • It is not the case, but in your question I see "initWithStyle:UITableViewCellStyleDefault" instead of UITableViewCellStyleSubtitle. – malex Jul 19 '14 at 16:07
0

The only working solution for your case (without subclassing of the UITableViewCell of course) is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ChatListItem";

    NSDictionary *dict = [_tableData objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = dict[@"text"];
    cell.detailTextLabel.text = dict[@"user"];

    if ([dict[@"user"] isEqualToString:@"(null)"]) {

        [self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];
    }

    return cell;
}

- (void)alignText:(UITableViewCell*)cell
{
    CGRect frame = cell.textLabel.frame;
    frame.origin.x = cell.frame.size.width - (frame.size.width + 10.0);
    cell.textLabel.frame = frame;

    frame = cell.detailTextLabel.frame;
    frame.origin.x = cell.frame.size.width - (frame.size.width + 10.0);
    cell.detailTextLabel.frame = frame;

    [cell setNeedsDisplay];
}

As for me, I would better make a subclass.

malex
  • 9,874
  • 3
  • 56
  • 77
  • Why do you add `(-frame.origin.x)` to `frame.origin.x`? This is always 0. Also, this is not "the only solution". – Mundi Jul 19 '14 at 15:52
  • You are right, the meaning of the frame change is simpler. Thanks. – malex Jul 19 '14 at 15:55
  • You are assuming the `cell.frame` is the same as `cell.contentView.frame`. This is not necessarily so. – Mundi Jul 19 '14 at 15:59
  • 1
    I only want to describe the approach to change frame. The actual sizes of cell or cell.contentView are secondary. For example, in the comment above you offered to use tableView:willDisplayCellForIndexPath: to change frame. I tried too, but it doesn't work. So I propose the idea to do it for sure. – malex Jul 19 '14 at 16:04
  • This approach is working, also I noted that it is better to subclass to get more canonical code. So, someone is using strange criteria to down vote answers. – malex Jul 19 '14 at 16:09
  • I gave this a shot and it seemed to work, but then clicking a cell caused that cell to revert back to the normal left alignment. Still, I up-voted the answer as I don't think it deserves to be down-voted. – primehalo Aug 11 '14 at 07:19