xcode 使所有文本显示在 UITableView 单元格中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11874323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making all the text show in a UITableView Cell
提问by Craig
I am loading an array into a UITableView and the components of the array are long. What I want to do is customize the cells so that thier lenght will adjust to fit the text but the width will stay the same. Right now what is happening is when the text gets too long it will just write off the cell and you will see this.
我正在将一个数组加载到 UITableView 中,并且该数组的组件很长。我想要做的是自定义单元格,以便它们的长度会调整以适合文本,但宽度将保持不变。现在发生的事情是,当文本变得太长时,它只会注销单元格,您会看到这一点。
I would like to see the text hit the end of the cell and then start a new line.
我希望看到文本到达单元格的末尾,然后开始新的一行。
My code right now looks like this.
我的代码现在看起来像这样。
@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;
}
I also found some code that somebody else wrote for multiple lined cells but don't know how to make it automatic based on the length of my string from the array.
我还发现了一些其他人为多行单元格编写的代码,但不知道如何根据数组中字符串的长度使其自动执行。
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;
They said that you will also need to return a suitable height for the multi-line cell and that a height of (44.0 + (numberOfLines - 1) * 19.0) should work fine.
他们说您还需要为多行单元格返回合适的高度,并且 (44.0 + (numberOfLines - 1) * 19.0) 的高度应该可以正常工作。
Does anyone have any ideas of how to do this?
有没有人对如何做到这一点有任何想法?
Thanks.
谢谢。
回答by Peter Pajchl
You will need to work with following methods
您将需要使用以下方法
- (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;
}
回答by Dancreek
You want to look at the NSString function -(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
你要看看NSString函数 -(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
You will have to include it in your heightForRowAtIndexPath
function to determine the right heights.
您必须将其包含在您的heightForRowAtIndexPath
函数中以确定正确的高度。
Also, set the numberOfLines
for your label to 0. This allows it to adjust to whatever number of lines is needed.
此外,将numberOfLines
您的标签的设置为 0。这允许它根据需要的行数进行调整。
回答by Feel Physics
How about using UITextView in table view cell.
如何在表格视图单元格中使用 UITextView。
At first, you have to change the height of cell.
首先,您必须更改单元格的高度。
Next, you put the UITextView into the cell.
接下来,您将 UITextView 放入单元格中。
Off course you can do this not by IB but by code.
当然,您可以通过代码而不是 IB 来做到这一点。
回答by Ayush Nawani
For me the accepted answer worked by adding the else condition.
In my case, if condition was not getting satisfied as the [tableView dequeueReusableCellWithIdentifier:@"comment"];
was always returning cell which was not nil.
对我来说,接受的答案通过添加 else 条件起作用。在我的情况下,如果条件没有得到满足,因为[tableView dequeueReusableCellWithIdentifier:@"comment"];
总是返回不为零的单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger item = indexPath.item;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"comment"];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
// added else condition
else {
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
[[cell textLabel] setText:commentsText[item]];
return cell;
}