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

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

概述我正在将数组加载到UITableView中,并且数组的组件很长.我想要做的是自定义单元格,以便它们的长度将调整以适应文本,但宽度将保持不变.现在发生的事情是,当文本太长时,它只会写下单元格,你会看到这个. 我想看到文本到达单元格的末尾,然后开始一个新行. 我的代码现在看起来像这样. @implementation CRHCommentSection@synthesize observationT 我正在将数组加载到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单元格中全部内容,希望文章能够帮你解决objective-c – 使所有文本显示在UITableView单元格中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存