0

我正在创建一个 Rss 提要应用程序。我需要在哪里显示描述(每个超过 5000 个字符)。我能够在我的应用程序中显示所有提要,但是当我尝试滚动 UITableView 时出现问题,滚动不流畅我相信我需要为我的内容进行延迟加载,但我是 iphone 开发的新手。谁能告诉我正确的方法。并根据内容大小设置正确的单元格大小。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell  *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


    }


    MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
    if (item) {

        if(cell.gestureRecognizers.count==0){
            UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longSelection:)];
            longPress.minimumPressDuration=1.0;
            [cell addGestureRecognizer:longPress];


        }

        UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
        UIImage *image=[UIImage imageNamed:@"Background.png"];

        imageView.image=image;
        cell.backgroundView=imageView;
        [[cell  textLabel] setBackgroundColor:[UIColor clearColor]];

        cell.textLabel.textAlignment=NSTextAlignmentRight;
        cell.textLabel.text=item.content;
        cell.textLabel.numberOfLines=0;
        cell.textLabel.font=[UIFont systemFontOfSize:15.0];
        [cell.textLabel sizeToFit];


        }

    return cell;



}

谢谢

4

1 回答 1

0

您必须使用-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

像这样:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
    //make sure to add the font you're using
    CGSize stringSize = [item.content sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:14]
                                     constrainedToSize:CGSizeMake(320, MAXFLOAT)
                                         lineBreakMode:UILineBreakModeWordWrap];

    int rowHeight = stringSize.height;

    return rowHeight;
}

把这段代码:

    UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
    UIImage *image=[UIImage imageNamed:@"Background.png"];

    imageView.image=image;
    cell.backgroundView=imageView;

像这样在此处将其从现在的位置删除(将其保留在那里会导致您每次滚动时都会创建新的图像视图:

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
        UIImage *image=[UIImage imageNamed:@"Background.png"];

        imageView.image=image;
        cell.backgroundView=imageView;

    }
于 2013-01-21T19:00:03.163 回答