【发布时间】:2011-04-21 15:02:23
【问题描述】:
当我使用 UITableViewCellStyleValue1 时,我得到了一长串 textLabel,并且不知何故 detailTextLabel 从视图中被推出。
当我把我的 textLabel 文本做短时,我可以看到 detailTextLabel 的文本。
有没有办法限制上述样式中的textLabel的宽度,以便它会截断太长的textLabel?
我的代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [self.currencyNameIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
self.currencyList = [self.keyCurrencyName filteredArrayUsingPredicate:predicate];
if ([self.currencyList count] > 0)
{
NSString *currencyName = [self.keyCurrencyName objectAtIndex:indexPath.row];
cell.textLabel.text = currencyName;
NSString *currencyCode = [self.valueCurrencyCode objectAtIndex:indexPath.row];
cell.detailTextLabel.text = currencyCode;
}
return cell;
}
所以我的货币名称在某些条目上会很长。
【问题讨论】:
-
是不是和Table View Programming Guide for iOS中的图1-8一样?
-
是的。正确的尼克。除了我的 detailTextLabel 固定为 3 个字符,但 textLabel 会很长,所以我需要截断 textLabel,如图 1-8 所示
-
奇怪,它应该默认截断。你设置了textLabel的linebreakmode了吗?
-
粘贴了代码。是的,也尝试设置换行模式,但没有运气
-
更奇怪。可以发截图吗?并请转储文本看起来不正确的单元格的 textLabel 框架。
标签: iphone uitableview textlabel