IOS - text alignment UITableViewCell

I added a tableView and dragged a table view cell into it. in the Utilities panel, I changed the style to subtitles. I also tried changing it in code:

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

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

    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

    cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [myArray2 objectAtIndex:indexPath.row];

    return cell;

Center Alignment Doesn't Work!

I tried adding a shortcut object to a cell in order to have a workaround. But I do not know how to access it. although I assigned her a way out, this did not work:

cell.labelForCell....

What should I do? Any suggestions on how I can get it to work in the normal way without adding a shortcut to the cell or something else?

+5
source share
6 answers

For UITableViewCellStyleSubtitletext alignment cannot be changed You will need to place a shortcut and add alignment to it. For this, you can use this code

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

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

        myLabel = [[UILabel alloc] initWithFrame:Your_Frame];
        //Add a tag to it in order to find it later
        myLabel.tag = 111;
        //Align it
        myLabel.textAlignment= UITextAlignmentCenter;

        [cell.contentView addSubview:myLabel];
    }

    myLabel = (UILabel*)[cell.contentView viewWithTag:111];
    //Add text to it
    myLabel.text = [myArray objectAtIndex:indexPath.row];

    return cell;
}
+18

, [self.textLabel sizeToFit], . , , .

. UITableViewCell textAlignment layoutSubviews:

- (void)layoutSubviews
{
    [super layoutSubviews];

    {
        CGRect frame = self.textLabel.frame;
        frame.size.width = CGRectGetWidth(self.frame);
        frame.origin.x = 0.0;
        self.textLabel.frame = frame;
    }

    {
        CGRect frame = self.detailTextLabel.frame;
        frame.size.width = CGRectGetWidth(self.frame);
        frame.origin.x = 0.0;
        self.detailTextLabel.frame = frame;
    }
}

textLabel , , .

: layoutSubviews, , .

+1

, , ,

cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;

.

+1

, , : textLabel , , , 0, , .

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textColor = [UIColor redColor];
        cell.detailTextLabel.textColor = [UIColor greenColor];
        cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
        cell.textLabel.numberOfLines = 0;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"You can initialize a very long string for the textLabel, or you can set the font to be a large number to make sure that the text cann't be shown in a singal line totally:%d", indexPath.row];
    return cell;

}

0

, . UITableViewCellStyleValue2, textLabel, , textAlignment , textLabel.

 -(void)layoutSubviews{
        [super layoutSubviews];

        if ([self.reuseIdentifier isEqualToString:@"FooterCell"]) {
            CGRect aTframe  = self.textLabel.frame;
            aTframe.size.width += 40;
            self.textLabel.frame = aTframe;

            CGRect adTframe  = self.detailTextLabel.frame;
            adTframe.origin.x += 70;
            self.detailTextLabel.frame = adTframe;        
        }
    }
0

textLabel detailTextLabel UITableViewCell cellForRowAtIndexPath: .

,

performSelector:withObject:afterDelay:

layoutSubviews:

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

.

0

All Articles