加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 使所有文本显示在UITableView单元格中

发布时间:2020-12-14 17:16:00 所属栏目:百科 来源:网络整理
导读:我正在将数组加载到UITableView中,并且数组的组件很长.我想要做的是自定义单元格,以便它们的长度将调整以适应文本,但宽度将保持不变.现在发生的事情是,当文本太长时,它只会写下单元格,你会看到这个. 我想看到文本到达单元格的末尾,然后开始一个新行. 我的代
我正在将数组加载到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-LinenText";
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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读