Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtitle alignment in UITableViewCell

i`m trying to build a table view with subtitle text in cells

the problem is when i try to set the alignment of the subtitle text to the right it doesn't work, but it works fine with the main text

here is my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

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

    [[cell textLabel] setTextAlignment:UITextAlignmentRight];
    [[cell detailTextLabel] setTextAlignment:UITextAlignmentRight];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.detailTextLabel.text = @"test";
    return cell;
}

when i remove the subtitle code the alignment works fine

any idea ?

like image 727
aLFaRSi Avatar asked Oct 26 '25 06:10

aLFaRSi


1 Answers

Ok so subclass UITableView Cell and customize the labels with init. You can override layoutSubviews and move the labels to the right:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0.0, 68.0, 80.0, self.frame.size.height);
    self.detailTextLabel.frame = CGRectMake(0.0, 68.0, 120.0, self.frame.size.height);
}

These are just example values so you can get the idea.

like image 111
self Avatar answered Oct 29 '25 07:10

self



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!