我有一個稍長的字符串,在tablView單元格中顯示時會被截斷。我這樣做是爲了使表格視圖變寬:如何在一個表格中顯示多行查看單元格
tableView.rowHeight = 100;
如何使字體變小以及將表格視圖單元格中的文字換行?
我有一個稍長的字符串,在tablView單元格中顯示時會被截斷。我這樣做是爲了使表格視圖變寬:如何在一個表格中顯示多行查看單元格
tableView.rowHeight = 100;
如何使字體變小以及將表格視圖單元格中的文字換行?
在tableView:cellForRowAtIndexPath:
中,您可以在textLabel(或descriptionLabel,取決於您使用的單元格樣式)上設置一些屬性來執行此操作。設置font
更改字體,linkBreakMode
使其自動換行,並numberOfLines
設置多少行最大(在該點之後截斷它,你可以設置爲0則沒有最大
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if(aCell == nil) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
aCell.textLabel.numberOfLines = 2; // 0 means no max.
}
// ... Your other cell setup stuff here
return aCell;
}
你應該子類UITableViewCell,並使其具有UITextView而不是UITextField。然後將你的子類分配給你的UITableView。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* Cell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
if(Cell == nil) {
Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
Cell.textLabel.font = [UIFont fontWithName:@"TimesNewRoman" size:10.0];
Cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
Cell.textLabel.numberOfLines = 2; // 0 means no max.
}
// your code here
return Cell;
}
這在iOS6中不推薦使用。你如何在iOS6中使用??? – Napolux 2012-09-27 20:50:13
據我所知,iOS6中唯一棄用的是'UILineBreakModeWordWrap','NSLineBreakByWordWrapping'是iOS6的等價物,更新了我的答案。 – zpasternack 2012-09-27 21:16:02