我正在将一个数组加载到 UITableView 中,并且该数组的组件很长。我想要做的是自定义单元格,以便调整它们的长度以适合文本,但宽度将保持不变。现在正在发生的事情是,当文本变得太长时,它只会注销单元格,你会看到这一点。
我希望看到文本到达单元格末尾,然后另起一行。
我的代码现在看起来像这样。
@implementation CRHCommentSection
@synthesize observationTable;
NSArray *myArray;
- (void)viewDidLoad
{
myArray = [CRHViewControllerScript theArray];
NSLog(@"%@", myArray);
//NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]];
//[observationTable insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[observationTable reloadData];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
NSLog(@" in method 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@" in method 2");
// Return the number of rows in the section.
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" in method 3");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
//cell.textLabel.text = @"Label";
return cell;
}
我还发现了一些其他人为多行单元格编写的代码,但不知道如何根据数组中字符串的长度使其自动生成。
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Label';
cell.detailTextLabel.text = @"Multi-Line\nText";
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
他们说您还需要为多行单元格返回一个合适的高度,并且 (44.0 + (numberOfLines - 1) * 19.0) 的高度应该可以正常工作。
有没有人知道如何做到这一点?
谢谢。
最佳答案
您将需要使用以下方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method is called for each cell and returns height
NSString * text = @"Your very long text";
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize: 14.0] forWidth:[tableView frame].size.width - 40.0 lineBreakMode:NSLineBreakByWordWrapping];
// return either default height or height to fit the text
return textSize.height < 44.0 ? 44.0 : textSize.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @"YourTableCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
[[cell textLabel] setText:@"Your very long text"];
return cell;
}
关于objective-c - 使所有文本显示在 UITableView 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11874323/