Xcode 表格单元格标签大小

Xcode 表格单元格标签大小,xcode,uitableview,Xcode,Uitableview,我有一个表格,我正在扩展以显示“更多信息”文本。我遇到的问题是文本的大小可能不同,即indexath.section1可能有1200高,而as index path.section2可能有300高 我的代码可以正常工作,直到重新使用单元格,标签框的大小不会根据新标签进行调整。因此,尽管单元格大小正确,但该单元格内的标签仍具有原始大小的框架,因此,如果太小,文本会被截断,我会看到大量的空白,如果太大,则除非我滚动到字段中间,否则我看不到文本 我做错了什么??有什么帮助吗 这是我的代码:-

我有一个表格,我正在扩展以显示“更多信息”文本。我遇到的问题是文本的大小可能不同,即indexath.section1可能有1200高,而as index path.section2可能有300高

我的代码可以正常工作,直到重新使用单元格,标签框的大小不会根据新标签进行调整。因此,尽管单元格大小正确,但该单元格内的标签仍具有原始大小的框架,因此,如果太小,文本会被截断,我会看到大量的空白,如果太大,则除非我滚动到字段中间,否则我看不到文本

我做错了什么??有什么帮助吗

这是我的代码:-

            static NSUInteger const k1 = 1;

            // Declare references to the subviews which will display the data.
            UILabel *lbl1 = nil;

            static NSString *kCellID = @"CellID";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];

                if ((indexPath.section == 8) || (indexPath.section == 9) || (indexPath.section == 10) || (indexPath.section == 11) || (indexPath.section == 12) || (indexPath.section == 13) || (indexPath.section == 14)|| (indexPath.section == 15) || (indexPath.section == 16))
                {
                    lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 200)];
                }

                else if ((indexPath.section == 4) || (indexPath.section == 5))
                {
                    lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 1200)];
                }
                lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
                lbl1.tag = k1;
                lbl1.textColor = [UIColor blackColor];
                lbl1.backgroundColor = [UIColor clearColor];
                lbl1.highlightedTextColor = [UIColor whiteColor];
                lbl1.opaque = NO;
                lbl1.adjustsFontSizeToFitWidth = TRUE;
                lbl1.numberOfLines = 0;

                lbl1.lineBreakMode = UILineBreakModeWordWrap;
                [cell.contentView addSubview:lbl1];

            } else {
                lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
            }

            cell.accessoryView.tag = indexPath.row;

            lbl1.text = [NSString stringWithFormat:@"%@", cellValue];

            return cell;
        }
    }
}

您需要在标签上设置自动调整大小的遮罩。这将导致在调整其superview的大小时调整其大小。适当的设置是:

lbl1.autoresizingMask = UIViewAutoresizingFlexibleHeight;

您没有包含名称,因此可以安全地假设此处显示的函数是“
[UITableViewDataSource tableView:cellForRowAtIndexPath:
方法吗?别担心,我通过更改-UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:nil]解决了此问题;现在可以正常工作了!!您真的应该使用标识符,因为表视图经过了优化以使用这些标识符(就重用屏幕上滚动的单元格而言)。我担心使用“
nil
“就像这样,特别是如果你的表格中有很多单元格。谢谢,我也意识到我不需要重复使用这些单元格。正如Michael在对你的问题的评论中所说,你真的应该重复使用这些单元格。