iDe*_*Dev 4 iphone objective-c uitableview ios
我想在UITableView中包装单元格的文本.我正在使用以下代码,但是现在如何动态更改单元格的高度,单元格的对齐现在不正确?我注意到tableView委托中有一个内置方法 - (CGFloat)cellHeightForRow但我可以动态接收文本并设置高度,因为我的JSON数据中有可变长度的文本
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
[cell.textLabel sizeToFit];
cell = [[[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleSubtitle
reuseIdentifier: @"UITableViewCell"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSDictionary *person = [myPeople objectAtIndex:[indexPath row]];
NSString *name = [person valueForKey:@"name"];
cell.detailTextLabel.text = [person valueForKey:@"time"];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *person = [myPeople objectAtIndex:[indexPath row]];
NSString *cellText =[person valueForKey:@"text"];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int buffer = 10;
return labelSize.height + buffer;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [commentView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 6;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
[cell.textLabel setMinimumFontSize:13.0];
[cell.textLabel setAdjustsFontSizeToFitWidth:NO];
}
NSDictionary *person = [myPeople objectAtIndex:[indexPath row]];
NSString *personName = [person valueForKey:@"text"];
cell.textLabel.text = personName;
// cell.detailTextLabel.text = [person valueForKey:@"date"];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
输出仍然看起来太紧凑和紧张
Sri*_*aju 15
你的内部cellForRowAtIndexPath:功能.第一次创建单元格时:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21.0];
}
Run Code Online (Sandbox Code Playgroud)
你会注意到我也将标签的行数设置为0.这使得它可以根据需要使用尽可能多的行.
你还需要指定你的大小UITableViewCell,所以在你的heightForRowAtIndexPath函数中这样做:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @"some text which is part of cell display";
UIFont *cellFont = [UIFont fontWithName:@"HelveticaNeue" size:21.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int buffer = 10;
return labelSize.height + buffer;
}
Run Code Online (Sandbox Code Playgroud)
我10在返回的单元格高度上添加了一个额外的,因为我喜欢在文本周围添加一点缓冲区.
更新:如果输出看起来太紧和结块,那么这样做 -
[cell.textLabel setMinimumFontSize:13.0];
[cell.textLabel setAdjustsFontSizeToFitWidth:NO];
Run Code Online (Sandbox Code Playgroud)
这应该可以解决您的问题.
| 归档时间: |
|
| 查看次数: |
4721 次 |
| 最近记录: |