Workaround to get textAlignment for center in UITableViewCellStyleSubtitle

According to this question (and many many google search results), when we are in the table view and we set the cells to UITableViewCellStyleSubtitle, we were forced to align our texts inside the cell label on the left.

However, I want one of the rows (actually the row) of my table view to be centered ... Obviously this did not work:

cell.textLabel.textAlignment = UITextAlignmentCenter;

So, I thought what I should do instead ... I'm not sure how to do this cleanly. The best way I can think of is to change this particular cell so that the cell style is UITableViewCellStyleDefault, but I don't think the cell property is volatile. Another way I can think of is to subclass the class UITableViewCell, but this seems a little redundant for just one cell of the table view. What do you think I should do to make this center with the last lines?

EDIT: for those of you who are interested in the answer:

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierDefault = @"CellDefault";

UITableViewCell *cell; // = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];  

  if (indexPath.row >= [data count])
        cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifierDefault];
    else
        cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil && indexPath.row < [data count]) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    else if(cell == nil && indexPath.row >= [data count]) { 
        //Use default style for the last cell.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierDefault] autorelease];
    }
+3
source share
3 answers

@the_great_monkey:

You can change cellIdentifierfor the cell in which you want to center alignment.

cellIdentifier.

,

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

, cellIdentifier , ?

, ,

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

, .

+2

.xib, . , UITableViewCell textLabel detailTextLabel IBOutlets, , IB.

- -tableView: cellForRowAtIndexPath:. , . , UITableViewCell, File Owner , , File Owner. . TableView Nib.

- UITableViewCell . , , , UITableViewCell. , .xib, , - . , , .

0
cell.textLabel.textAlignment=UITextBorderStyleLine;
0
source

All Articles