1

我有 UITable 视图,在合适的单元格中,我使用自己的标签和非常长的文本。结果这个 uilabel 花费了大约 100 mb。(在这种情况下 - 内存警告然后崩溃)。我该如何解决?提前致谢。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UILabel *message;
    CGSize textSize;
    CGSize fullSize = CGSizeMake(300, 100000);
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    message = [[UILabel alloc] init];
    message.frame = CGRectMake(10, 30, 100, 20);
    message.font = [UIFont systemFontOfSize:12.0f];
    message.textAlignment = UITextAlignmentLeft;
    message.textColor = [UIColor whiteColor];
    message.tag = 103;
    message.numberOfLines = 100000;
    message.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:message];
    [message release];
}
    else
{
    message = (UILabel *)[cell.contentView viewWithTag:103];
}
NSData *messageData = [[grandData objectAtIndex:indexPath.row] objectForKey:@"content"];
NSString *tempContent = [[NSString alloc] initWithData:messageData encoding:NSUTF8StringEncoding];
message.text = tempContent;   // text about 10000 symbols in length
[tempContent release];
   textSize = [message.text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:fullSize lineBreakMode:UILineBreakModeWordWrap];
message.frame = CGRectMake(10, 30, textSize.width, textSize.height + 6);

return cell;

}

我使用消息标签(不是 cell.textLabel),因为有一些标签显示其他信息(我削减了多余的代码)。单元格的高度会根据文本的数量动态变化

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
4

2 回答 2

1

您正在泄漏内存。

创建时:

NSString *tempContent = [[NSString alloc] initWithData:messageData encoding:NSUTF8StringEncoding];

之后你不会释放tempContent

您应该将代码更改为以下内容:

NSData *messageData = [[grandData objectAtIndex:indexPath.row]
                       objectForKey:@"content"];
NSString *tempContent = [[NSString alloc] initWithData:messageData   
                                              encoding:NSUTF8StringEncoding];
message.text = tempContent;   // text about 10000 symbols in length
[tempContent release];
于 2012-08-17T15:03:32.303 回答
0

尽管我使用 dequeueReusableCell,但我对 Swift 3 也有同样的问题

class StepTableViewCell: UITableViewCell {

    @IBOutlet weak var stepImageView: UIImageView!
    @IBOutlet weak var stepNumberLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
 }


    var fontSize = CGFloat(14)
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    customCell = tableView.dequeueReusableCell(withIdentifier: "StepCellWithoutImage", for: indexPath) as!  StepTableViewCell


                    customCell.stepNumberLabel.text = "\(cellInfo.index + 1)"
                    customCell.contentLabel.font =  UIFont.systemFont(ofSize: fontSize)
                    customCell.contentLabel.text = step.content
                    debugPrint(step.content?.count)

                    return  customCell

}

问题实际上是当它尝试增加字体并重新加载 tableView

于 2018-01-15T10:02:27.290 回答