Girish
Girish

Reputation: 9

How to use UITextAlignment to align text in UITableViewCell to the right and left in Swift

I have to align text in table cell to right and left.I have used two plist in my application according to plist selection I have to align my text in table cell.

I try following code for that

if ([app.currentPlist isEqualToString:@"Accounting.plist"]) {
    cell.textAlignment=UITextAlignmentLeft;            
} else {
    cell.textAlignment=UITextAlignmentRight;           
}

Upvotes: 0

Views: 8465

Answers (3)

Rupal Chawla
Rupal Chawla

Reputation: 91

UITextAlignmentRight is deprecated, use NSTextAlignmentLeft instead

So, the code will be - cell.textLabel.textAlignment = NSTextAlignmentLeft;

Upvotes: 2

Bala Vishnu
Bala Vishnu

Reputation: 2628

In swift it is

cell.textLabel.textAlignment = NSTextAlignment.Right

Upvotes: 1

Alex Coplan
Alex Coplan

Reputation: 13361

UITableViewCell doesn't have a textAlignment property. You have to set it on the cell's text label:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.text = @"Something";

    if([app.currentPlist isEqualToString:@"Accounting.plist"])
    {
        cell.textLabel.textAlignment=UITextAlignmentLeft;             
    }
    else
    {
        cell.textLabel.textAlignment=UITextAlignmentRight;           
    }

    return cell;
}

Upvotes: 2

Related Questions