根据this question(以及许多Google搜索结果),当我们处于表格视图中,并且我们将单元格设置为UITableViewCellStyleSubtitle
时,我们不得不调整文本在单元格标签内部位于左侧。解决方法:将textAlignment设置为UITableViewCellStyleSubtitle的中心
但是我想行的一个(其实就是行)我表视图的对齐中心......显然,这是行不通的:
cell.textLabel.textAlignment = UITextAlignmentCenter;
所以我一直在想什么,我应该做,而不是...我不知道如何干净地做到这一点。我能想到的最好办法是改变这个特定的单元格,以便单元格的样式为UITableViewCellStyleDefault
,但我不认为单元格属性是可更改的。我能想到的另一种方法是对UITableViewCell
类进行子类化,但对于一个表视图单元格来说,这看起来有点矫枉过正。你认为我应该怎么做才能让最后一排对齐中心?
编辑:对于那些谁感兴趣的答案:
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];
}
我想你应该只是最后一个单元格'UITableViewCellStyleDefault'。它比上面提到的任何事情都容易得多。 – Robin 2011-03-18 04:49:26
但是我需要字幕......如果我使用默认样式,如何显示text.detailTextLabel? – 2011-03-18 04:54:04
你必须只为最后一行而不是任何其他行(如你在你的问题中提问) – Robin 2011-03-18 04:59:09